using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

namespace Valkyrie
{
    public class BasicShaderGUI : ShaderGUI
    {
        private static class Styles
        {
            public static readonly GUIContent IlluminLabel = new GUIContent("Illumin","");
            public static string IlluminText = "Illumin:"; 
            public static GUIContent IlluminOnLabel = EditorGUIUtility.TrTextContent("Illumin On", "自发光动画");
            public static GUIContent TimeScaleLabel = EditorGUIUtility.TrTextContent("Time Scale", "时间");
            public static GUIContent IlluminStrengthLabel = EditorGUIUtility.TrTextContent("Illumin Strength", "自发光");
            public static GUIContent IlluminColorLabel = EditorGUIUtility.TrTextContent("Illumin Color", "自发光颜色");
            
            public static readonly GUIContent ColorationLabel = new GUIContent("Coloration","");
            public static string colorationText = "Coloration:";
            public static GUIContent isColorationLabel = EditorGUIUtility.TrTextContent("Is Coloration","是否染色"); //_IsColoration
            public static string whiteText = "White Channel:";
            public static GUIContent hueShiftLabel = EditorGUIUtility.TrTextContent("HueShiftW","色相"); 
            public static GUIContent satLabel = EditorGUIUtility.TrTextContent("SaturationW","饱和"); 
            public static GUIContent valLabel = EditorGUIUtility.TrTextContent("BrightnessW","明度");
            public static GUIContent colorationLabel = EditorGUIUtility.TrTextContent("Coloration","染色颜色"); //_Coloration
            public static GUIContent whiteChannelLabel = EditorGUIUtility.TrTextContent("White Channel","开启纯白通道"); 
            public static string grayText = "Gray Channel:";
            public static GUIContent hueShiftGLabel = EditorGUIUtility.TrTextContent("HueShiftG","色相");
            public static GUIContent satGLabel = EditorGUIUtility.TrTextContent("SaturationG","饱和"); 
            public static GUIContent valGLabel = EditorGUIUtility.TrTextContent("BrightnessG","明度"); 
            public static GUIContent colorationGLabel = EditorGUIUtility.TrTextContent("ColorationG","染色颜色"); //_Coloration

            public static readonly GUIContent OutlineLabel = new GUIContent("Outline","");
            public static GUIContent TangentLabel = EditorGUIUtility.TrTextContent("Tangent","切线");
            public static GUIContent OutlineColorLabel = EditorGUIUtility.TrTextContent("Outline Color",""); //_Outline
            public static GUIContent outlineLabel = EditorGUIUtility.TrTextContent("Outline Thickness","勾边厚度"); 
            public static GUIContent OutlineStrengthLabel = EditorGUIUtility.TrTextContent("Outline Strength","勾边强度 "); 

            public static readonly GUIContent RimLabel = new GUIContent("Rim","");
            public static string rimText = "[Ramp95%为Rim颜色]";    
            public static GUIContent BackRimColorLabel = EditorGUIUtility.TrTextContent("Back Rim Color","背光 ");    
            public static GUIContent BackRimMaxLabel = EditorGUIUtility.TrTextContent("Back Rim Max","背光最大值");
            public static GUIContent BackRimMinLabel = EditorGUIUtility.TrTextContent("Back Rim Min","背光最小值");
            public static string flashText = "Flash:";
             public static GUIContent flashSwitchLabel = EditorGUIUtility.TrTextContent("FLASH SWITCH","流光开关");
            public static GUIContent flashLabel = EditorGUIUtility.TrTextContent("Flash(RG)","闪光贴图，变体"); //_FlashTex
            public static GUIContent flashFactorLabel = EditorGUIUtility.TrTextContent("Flash Factor(xy:UV动画偏移方向,zw:UV缩放)"); //_FlashFactor
            public static GUIContent flashStrengthLabel = EditorGUIUtility.TrTextContent("Strength","闪光强度:放大Flash贴图的R通道"); //_FlashStrength

            public static string BloomText = "[----------Bloom----------]";
            public static GUIContent BloomFactorLabel = EditorGUIUtility.TrTextContent("Bloom Factor", "控制受bloom影响程度， 越大影响越大"); 

        }
        
        MaterialProperty IlluminOn = null;
        MaterialProperty TimeScale = null;
        MaterialProperty IlluminStrength = null;
        MaterialProperty IlluminColor = null;

        MaterialProperty isColoration = null;
        MaterialProperty whiteChannel = null;
        MaterialProperty coloration = null;
        MaterialProperty colorationG = null;
        MaterialProperty hueShift = null;
        MaterialProperty hueShiftG = null;
        MaterialProperty sat = null;
        MaterialProperty satG = null;
        MaterialProperty val = null;
        MaterialProperty valG = null;

        bool m_ColorationFoldout;
        bool m_IlluminFoldout;
        //bool m_LightFoldout;

        MaterialProperty Tangent = null;
        MaterialProperty outline = null;
        MaterialProperty OutlineColor = null;
        MaterialProperty OutlineStrength = null;
        MaterialProperty BackRimColor = null;
        MaterialProperty BackRimMax = null;
        MaterialProperty BackRimMin = null;
        MaterialProperty flashSwitch = null;
        MaterialProperty flashTex = null;
        MaterialProperty flashFactor = null;
        MaterialProperty flashStrength = null;
        MaterialProperty bloomFactor = null;

        MaterialProperty DissolveTex = null;

        bool m_RimFoldout;
        bool m_outlineFoldout;

        public MaterialEditor m_MaterialEditor;
        

        public void FindProperties(MaterialProperty[] props)
        {
            
            IlluminOn = FindProperty("_Illumin_On", props);
            TimeScale = FindProperty("_TimeScale", props);
            IlluminStrength = FindProperty("_IlluminStrength", props);
            IlluminColor = FindProperty("_IlluminColor", props);

            isColoration = FindProperty("_IsColoration", props);
            whiteChannel = FindProperty("_WhiteChannel", props);
            coloration = FindProperty("_Coloration", props);
            colorationG = FindProperty("_ColorationG", props);
            hueShift = FindProperty("_HueShift", props);
            hueShiftG = FindProperty("_HueShiftG", props);
            sat = FindProperty("_Sat", props);
            satG = FindProperty("_SatG", props);
            val = FindProperty("_Val", props);
            valG = FindProperty("_ValG", props);

            Tangent = FindProperty("_Tangent", props);
            outline = FindProperty("_Outline", props);
            OutlineColor = FindProperty("_OutlineColor", props);
            OutlineStrength = FindProperty("_OutlineStrength", props);
            BackRimColor = FindProperty("_BackRimColor", props);
            BackRimMax = FindProperty("_BackRimMax", props);
            BackRimMin = FindProperty("_BackRimMin", props);
            flashSwitch = FindProperty("_FLASH_SWITCH", props);
            flashTex = FindProperty("_FlashTex", props);
            flashFactor = FindProperty("_FlashFactor", props);
            flashStrength = FindProperty("_FlashStrength", props);
            bloomFactor = FindProperty("_BloomFactor", props);
            DissolveTex = FindProperty("_DissolveTex", props);
        }
        
        public void ShaderPropertiesGUI(Material material)
        {
            EditorGUI.BeginChangeCheck();
            {

                m_IlluminFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(m_IlluminFoldout, Styles.IlluminLabel);
                if (m_IlluminFoldout)
                {
                    m_MaterialEditor.ShaderProperty(IlluminOn, Styles.IlluminOnLabel);
                    m_MaterialEditor.ShaderProperty(TimeScale, Styles.TimeScaleLabel);
                    m_MaterialEditor.ShaderProperty(IlluminStrength, Styles.IlluminStrengthLabel);
                    m_MaterialEditor.ShaderProperty(IlluminColor, Styles.IlluminColorLabel);
                    EditorGUILayout.Space();
                }
                EditorGUILayout.EndFoldoutHeaderGroup();

                EditorGUI.BeginChangeCheck();
                m_ColorationFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(m_ColorationFoldout, Styles.ColorationLabel);
                if (m_ColorationFoldout)
                {
                m_MaterialEditor.ShaderProperty(isColoration, Styles.isColorationLabel);

                m_MaterialEditor.ShaderProperty(whiteChannel, Styles.whiteChannelLabel);
                if (whiteChannel.floatValue == 1){
                    m_MaterialEditor.ShaderProperty(hueShift, Styles.hueShiftLabel);
                    m_MaterialEditor.ShaderProperty(sat, Styles.satLabel);
                    m_MaterialEditor.ShaderProperty(val, Styles.valLabel);
                    m_MaterialEditor.ShaderProperty(coloration, Styles.colorationLabel);}  

                    GUILayout.Label(Styles.grayText, EditorStyles.boldLabel);  
                    m_MaterialEditor.ShaderProperty(hueShiftG, Styles.hueShiftGLabel);
                    m_MaterialEditor.ShaderProperty(satG, Styles.satGLabel);
                    m_MaterialEditor.ShaderProperty(valG, Styles.valGLabel);
                    m_MaterialEditor.ShaderProperty(colorationG, Styles.colorationGLabel);

                    EditorGUILayout.Space();
                }
                EditorGUILayout.EndFoldoutHeaderGroup();

                m_outlineFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(m_outlineFoldout, Styles.OutlineLabel);
                if (m_outlineFoldout)
                {
                    m_MaterialEditor.ShaderProperty(Tangent, Styles.TangentLabel);
                    m_MaterialEditor.ShaderProperty(OutlineColor, Styles.OutlineColorLabel);
                    m_MaterialEditor.ShaderProperty(outline, Styles.outlineLabel);
                    m_MaterialEditor.ShaderProperty(OutlineStrength, Styles.OutlineStrengthLabel);
                    EditorGUILayout.Space();
                }
                EditorGUILayout.EndFoldoutHeaderGroup();
                
                m_RimFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(m_RimFoldout, Styles.RimLabel);
                if (m_RimFoldout)
                {
                    GUILayout.Label(Styles.rimText, EditorStyles.boldLabel);
                    m_MaterialEditor.ShaderProperty(BackRimColor, Styles.BackRimColorLabel);
                    m_MaterialEditor.ShaderProperty(BackRimMax, Styles.BackRimMaxLabel);
                    m_MaterialEditor.ShaderProperty(BackRimMin, Styles.BackRimMinLabel);
                    EditorGUILayout.Space();
                }
                EditorGUILayout.EndFoldoutHeaderGroup();

                GUILayout.Label(Styles.BloomText, EditorStyles.boldLabel);
                m_MaterialEditor.ShaderProperty(bloomFactor, Styles.BloomFactorLabel);
                EditorGUILayout.Space();

                m_MaterialEditor.TexturePropertySingleLine(EditorGUIUtility.TrTextContent("DissolveDefaultTex", "溶解默认贴图 "),DissolveTex);

            }
            if (EditorGUI.EndChangeCheck())
            {
                
            }
        }

        public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
        {
            if (materialEditor == null)
                throw new ArgumentNullException("materialEditor");
            FindProperties(props);
            m_MaterialEditor = materialEditor;
            Material material = materialEditor.target as Material;
            ShaderPropertiesGUI(material);
            m_MaterialEditor.RenderQueueField();
            m_MaterialEditor.EnableInstancingField();
        }
        
    }
}
