using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using CommonLang.IO;
using System.Reflection;
using CommonUI_Unity3D.Impl;

namespace CommonUI_Unity3D.Impl
{
    public static class UnityShaders
    {
        public static void InitShaders()
        {
            Debug.Log("- InitShaders ");
            UI_IMG_BASE_Shader = new Material(Shader.Find("iPhone/MFUI_Image"));
            UI_IMG_GRAY_Shader = new Material(Shader.Find("iPhone/MFUI_ImageGray"));

            UI_M3Z_BASE_Shader = new Material(Shader.Find("iPhone/MFUI_ImageMask"));
            UI_M3Z_GRAY_Shader = new Material(Shader.Find("iPhone/MFUI_ImageMaskGray"));

            UI_TXT_BASE_Shader = new Material(Shader.Find("iPhone/MFUI_Text"));
            UI_TXT_GRAY_Shader = new Material(Shader.Find("iPhone/MFUI_TextGray"));

            UI_Shape_Shader = new Material(Shader.Find("iPhone/MFUI_Shape"));

            UI_IMG_BASE_Shader.mainTextureScale = new Vector2(1, 1);
            UI_IMG_GRAY_Shader.mainTextureScale = new Vector2(1, 1);
            UI_M3Z_BASE_Shader.mainTextureScale = new Vector2(1, 1);
            UI_M3Z_GRAY_Shader.mainTextureScale = new Vector2(1, 1);

            UI_IMG_BASE_Shader.color = UnityEngine.Color.white;
            UI_IMG_GRAY_Shader.color = UnityEngine.Color.white;
            UI_M3Z_BASE_Shader.color = UnityEngine.Color.white;
            UI_M3Z_GRAY_Shader.color = UnityEngine.Color.white;
            UI_TXT_BASE_Shader.color = UnityEngine.Color.white;
            UI_TXT_GRAY_Shader.color = UnityEngine.Color.white;

            UI_Shape_Shader.color = UnityEngine.Color.white;

            UI_Image_Shader = Shader.Find("MFUGUI/Image");
            UI_ImageM3Z_Shader = Shader.Find("MFUGUI/ImageM3Z");
            UI_TextGray_Shader = Shader.Find("MFUGUI/TextGray");
        }

        public static Shader UI_Image_Shader { get; private set; }
        public static Shader UI_ImageM3Z_Shader { get; private set; }
        public static Shader UI_TextGray_Shader { get; private set; }

        public static Material CreateMaterialUGUI(UnityImage text)
        {
            if (text.TextureMask == null)
            {
                Material ret = new Material(UI_Image_Shader);
                ret.mainTextureScale = new Vector2(1, 1);
                ret.color = UnityEngine.Color.white;
                ret.mainTexture = text.Texture;
                //ret.SetFloat("_Gray", 1);
                ret.SetPass(0);
                return ret;
            }
            else
            {
                Material ret = new Material(UI_ImageM3Z_Shader);
                ret.mainTextureScale = new Vector2(1, 1);
                ret.color = UnityEngine.Color.white;
                ret.mainTexture = text.Texture;
                ret.SetTexture("_MaskTex", text.TextureMask);
                //ret.SetFloat("_Gray", 1);
                ret.SetPass(0);
                return ret;
            }
        }

        public static Material UI_IMG_BASE_Shader { get; private set; }
        public static Material UI_IMG_GRAY_Shader { get; private set; }
        public static Material UI_M3Z_BASE_Shader { get; private set; }
        public static Material UI_M3Z_GRAY_Shader { get; private set; }
        public static Material UI_TXT_BASE_Shader { get; private set; }
        public static Material UI_TXT_GRAY_Shader { get; private set; }
        public static Material UI_Shape_Shader;

        private static Color mColorShape = Color.white;
        private static Color mColorImage = Color.white;
        private static CommonUI.Display.Blend mBlend = CommonUI.Display.Blend.BLEND_MODE_NORMAL;

        public static void BeginText(UnityTextLayer text)
        {
            switch (mBlend)
            {
                case CommonUI.Display.Blend.BLEND_MODE_GRAY:
                    UI_TXT_GRAY_Shader.mainTexture = text.mTexture;
                    UI_TXT_GRAY_Shader.SetPass(0);
                    break;
                default:
                    UI_TXT_BASE_Shader.mainTexture = text.mTexture;
                    UI_TXT_BASE_Shader.SetPass(0);
                    break;
            }
        }
        public static void BeginImage(IUnityImageInterface image)
        {
            if (image.TextureMask == null)
            {
                switch (mBlend)
                {
                    case CommonUI.Display.Blend.BLEND_MODE_GRAY:
                        UI_IMG_GRAY_Shader.mainTexture = image.Texture;
                        UI_IMG_GRAY_Shader.SetPass(0);
                        break;
                    default:
                        UI_IMG_BASE_Shader.mainTexture = image.Texture;
                        UI_IMG_BASE_Shader.SetPass(0);
                        break;
                }
            }
            else
            {
                switch (mBlend)
                {
                    case CommonUI.Display.Blend.BLEND_MODE_GRAY:
                        UI_M3Z_GRAY_Shader.mainTexture = image.Texture;
                        UI_M3Z_GRAY_Shader.SetTexture("_MaskTex", image.TextureMask);
                        UI_M3Z_GRAY_Shader.SetPass(0);
                        break;
                    default:
                        UI_M3Z_BASE_Shader.mainTexture = image.Texture;
                        UI_M3Z_BASE_Shader.SetTexture("_MaskTex", image.TextureMask);
                        UI_M3Z_BASE_Shader.SetPass(0);
                        break;
                }
            }
        }
        public static void BeginShape()
        {
            UI_Shape_Shader.SetPass(0);
        }


        public static void SetColor(UnityEngine.Color color)
        {
            mColorShape = color;
            UI_Shape_Shader.SetColor("_clrBase", mColorShape);
        }

        public static void SetAlpha(float a)
        {
            mColorShape.a = a;
            mColorImage.a = a;
            UI_IMG_BASE_Shader.SetColor("_clrBase", mColorImage);
            UI_IMG_GRAY_Shader.SetColor("_clrBase", mColorImage);
            UI_M3Z_BASE_Shader.SetColor("_clrBase", mColorImage);
            UI_M3Z_GRAY_Shader.SetColor("_clrBase", mColorImage);
            UI_TXT_BASE_Shader.SetColor("_clrBase", mColorImage);
            UI_TXT_GRAY_Shader.SetColor("_clrBase", mColorImage);
            UI_Shape_Shader.SetColor("_clrBase", mColorShape);
        }

        public static void SetBlend(CommonUI.Display.Blend blend)
        {
            mBlend = blend;
        }
    }
}