using o0.Geometry2D.Float;
using System;
using System.Linq;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Internal;

namespace ZIM
{
    public static partial class Extension
    {
        public static Vector2 UnityVector(this Vector v)
        {
            return new Vector2(v.x, v.y);
        }

        public static Vector o0Vector(this Vector2 v)
        {
            return new Vector(v.x, v.y);
        }

        public static UnityEngine.Color UnityColor(this o0.Color c)
        {
            return new UnityEngine.Color(c.r, c.g, c.b, c.a);
        }

        public static o0.Color o0Color(this UnityEngine.Color c)
        {
            return new o0.Color(c.r, c.g, c.b, c.a);
        }

        public static float LengthManhattan(this Vector2 v)
        {
            return Math.Abs(v.x) + Math.Abs(v.y);
        }

        public static float Brightness(this Color c)
        {
            return 0.59f * c.r + 0.3f * c.r + 0.11f * c.r;    // 红色为主
        }

        // 转换为整数再计算
        public static int Brightness(this Color c, int colorBits)
        {
            int r = (int)(c.r * colorBits);
            int g = (int)(c.g * colorBits);
            int b = (int)(c.b * colorBits);
            return (int)(0.59f * r + 0.3f * g + 0.11f * b);    // 红色为主
        }

        // 像素坐标映射到Unity局部坐标
        public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, Vector2 size, Rect dstRect)
        {
            var xp = pixel.x / size.x - 0.5f;
            var yp = pixel.y / size.y - 0.5f;
            //var origin = rawImage.anchoredPosition;
            return new Vector2(dstRect.width * xp, dstRect.height * yp);
        }

        // 像素坐标映射到Unity局部坐标
        public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, o0.Geometry2D.Vector<int> size, Rect dstRect)
        {
            var xp = pixel.x / size.x - 0.5f;
            var yp = pixel.y / size.y - 0.5f;
            //var origin = rawImage.anchoredPosition;
            return new Vector2(dstRect.width * xp, dstRect.height * yp);
        }

        public static bool IsInScreen(this OrdinalQuadrilateral Quad, Vector2 size)
        {
            if (Quad.A.x < Quad.B.x && Quad.A.y < Quad.C.y && Quad.B.y < Quad.D.y && Quad.C.x < Quad.D.x && Quad.A.x > 0 && Quad.A.y > 0 &&
                Quad.B.x < size.x && Quad.B.y > 0 && Quad.C.x > 0 && Quad.C.y < size.y && Quad.D.x < size.x && Quad.D.y < size.y)
                return true;
            return false;
        }

        public static float DegreeToXAxis(this Vector2 v)
        {
            var a = v.x > 0 ? Math.Atan(v.y / v.x) * 180 / Math.PI : 180 + Math.Atan(v.y / v.x) * 180 / Math.PI;
            a = a < 0 ? 360 + a : a;
            return (float)a;
        }

        public static Texture2D zimAutoLightSimple(this WebCamTexture texture)
        {
            var pixel = texture.GetPixels();
            return zimAutoLightSimple(pixel,texture.width,texture.height);
        }

        public static Texture2D zimAutoLightSimple(this Texture2D texture)
        {
            var pixel = texture.GetPixels();
            return zimAutoLightSimple(pixel, texture.width, texture.height);
        }

        public static Vector2 Size(this Texture2D texture)
        { 
            return new Vector2(texture.width, texture.height);
        }

    
        // 效果不太好
        public static Texture2D zimAutoLightSimple(this Color[] pixel, int width, int height)
        {
            Color[] p = new Color[pixel.Length];

            var brightness16 = new int[256];
            for (int i = 0; i < width / 7; i++) 
            {
                for (int j = 0; j < height / 7; j++) 
                {
                    var index = j * 7 * width + i * 7;
                    var b = pixel[index].Brightness(256);
                    brightness16[b]++;
                }
            }
            var limit = (int)(width * height / 49 * 0.001f);    // 取亮度大约在前0.1%的值（不取最大的因为可能会被个别亮点影响）
            int select = 255;
            for (; select >= 0; select--)
            {
                limit -= brightness16[select];
                if (limit <= 0)
                    break;
            }
            var scale = 0.85f / (select / 256f);

            //var avgBrightness = 0f;
            //Parallel.For(0, pixel.Length / 256, (i) => avgBrightness += pixel[i * 256].Brightness());
            //avgBrightness /= pixel.Length / 256;
            //var scale = 1 / avgBrightness / 2f;

            if (float.IsInfinity(scale))
                return null;
            Parallel.For(0, p.Length, (i) => p[i] = pixel[i] * scale);

            var tex = new Texture2D(width, height);
            tex.SetPixels(p);
            tex.Apply();
            return tex;
        }

        public static Texture2D zimAutoLight(this WebCamTexture texture)
        {
            var pixel = texture.GetPixels();
            return zimAutoLight(pixel, texture.width, texture.height,0);
        }
        public static Texture2D zimAutoLight(this Texture2D texture,int brightness)
        {
            var pixel = texture.GetPixels();
            return zimAutoLight(pixel, texture.width, texture.height, brightness);
        }
        public static Texture2D zimAutoLight(this Color[] pixel, int width, int height, [DefaultValue("0")] int brightness)
        {
            float[] p = new float[pixel.Length];
            Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].Brightness());

            var scale = 1 / Enumerable.Max(p) * (2 + brightness);
            if (float.IsInfinity(scale))
                return null;
            Parallel.For(0, p.Length, (i) => pixel[i] *= scale);        // 归一化

            var tex = new Texture2D(width, height);
            tex.SetPixels(pixel);
            tex.Apply();
            return tex;
        }

        public static Texture2D zimAutoLevelEqualization(this WebCamTexture texture, int bit = 256)
        {
            var pixel = texture.GetPixels();
            float[] p = new float[pixel.Length];
            Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].r);        // 红通道

            bit -= 1;
            var scale = 1 / Enumerable.Max(p);
            if (float.IsInfinity(scale))
                return null;
            Parallel.For(0, p.Length, (i) => p[i] *= scale);        // 归一化

            int[] pint = new int[p.Length];
            Parallel.For(0, p.Length, i => pint[i] = (int)(p[i] * bit));        // 映射到256色彩

            //Debug.Log(Enumerable.Max(pint) + ", " + scale);

            int[] H = new int[bit + 1];
            for (int i = 0; i < p.Length; i++)
                H[pint[i]]++;
            float[] HF = new float[bit + 1];
            for (int i = 0; i <= bit; i++)
                HF[i] = H[i] / (float)p.Length;       // 概率

            float[] D = new float[bit + 1];
            float temp = 0;
            for (int i = 1; i <= bit; i++)      // 不取颜色为0的概率
            {
                temp += HF[i];
                D[i] = temp;        // 累计密度
            }
            Parallel.For(0, p.Length, i => p[i] = D[pint[i]] / D.Last());       // 取密度为颜色，并归一化

            UnityEngine.Color[] pixelLighted = new UnityEngine.Color[pixel.Length];
            Parallel.For(0, pixel.Length, (i) => pixelLighted[i] = new UnityEngine.Color(p[i], 0, 0));
            var tex = new Texture2D(texture.width, texture.height);
            tex.SetPixels(pixelLighted);
            tex.Apply();
            return tex;
        }


        public static void CopyTo(this RenderTexture src, Texture2D dst)
        {
            var pre = RenderTexture.active;

            RenderTexture.active = src;
            dst.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
            dst.Apply();

            RenderTexture.active = pre;
        }
    }

}
