using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

//这个节点必须在现实的下边
class PosChecks
{
    public Vector2 pos;
    public bool isUse = false;
    public int type = 0;
    public float X
    {
        get
        {
            if (isUse == false) return -1000000f;
            else return pos.x;
        }
    }
    public float Y
    {
        get 
        {
            if (isUse == false) return -10000000f;
            else return pos.y;
        }
    }
}
public class checkRootFirstRun : MonoBehaviour
{
    // Start is called before the first frame update
    public int r1 = 60;
    public int r2 = 20;
    public int width = 500;
    public int height = 500;
    public Color color = Color.white;
    private Color backColor = new Color(1.0f,1.0f,0.23f,1);
    private int oldR1 = 0;
    private int oldR2 = 0;
    private Image image = null;
    private Color[] colors = null;
    private List<PosChecks> posChecks = new List<PosChecks>();
    private PosChecks tempPos = null;
    private bool colorIsChange = false;
    private float OldTouchCurrDis = 0;
    void Start()
    {
        posChecks.Add(new PosChecks());
        posChecks.Add(new PosChecks());
        posChecks.Add(new PosChecks());
        initMember();
    }
    void initMember()
    {
        if(colors == null)
        {
            Texture2D texture2D = null;
            texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
            Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
            image = transform.Find("Image").GetComponent<Image>();
            image.sprite = sprite;
            colors = image.sprite.texture.GetPixels();
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int idx = y * width + x;
                    colors[idx] = backColor;
                }
            }            
            texture2D.SetPixels(colors);
            texture2D.Apply();
        }
    }
    void drawView()
    {
        if(colors != null)
        {
           
        }
    }
    private void loopCheck(Transform trans)
    {
        
    }
    public void onApply()
    {
        ActionManages.addMoveByScale(transform.Find("Image/test").gameObject, 8f, 10f);
    }
    public void drawBlock(Vector2 pos1,Color color)
    {
        if (colors == null) return;
        int width = image.sprite.texture.width;
        int height = image.sprite.texture.height;
        int x = (int)pos1.x + width/2;
        int y = (int)pos1.y + height  /2;
        int r1 = 10;
        
        colorIsChange = true;
        for (int x1 = x - r1; x1 < x + r1; x1++)
        {
            if (x1 < 0 || x1 >= width) continue;
            for(int y1 = y - r1; y1 < y + r1; y1++)
            {
                if (y1 < 0 || y1 >= height) continue;
                colors[y1 * width + x1] = color;
            }
        }
    }
    public void setZhongXinValue(Vector2 vector)
    {
        int width = image.sprite.texture.width;
        int height = image.sprite.texture.height;
        int x = (int)vector.x + width / 2;
        int y = (int)vector.y + height / 2;
        float x1 =((float)x / (float)width);
        float y1 = ((float)y / (float)height);
        RectTransform gobject = image.transform.Find("test") as RectTransform;
        var an1 = new Vector2(x1, y1);
        Debug.Log(an1);
        utilsTools.setAnchor(gobject, an1);
        if(utilsTools.IsPhone == false){
            gobject.localScale = new Vector2(0.2f, 0.2f);
            ActionManages.addMoveByScale(gobject.gameObject, 9, 5f, (GameObject _, int id) =>{
                gobject.localScale = new Vector2(0.2f, 0.2f);
            });
        }
    }
    public void pushDrawPos()
    {
        int i = posChecks[0].isUse == false ? 0 : (posChecks[1].isUse == false ? 1 : -1);
        if(i == -1)
        {
            //先清理一边老的的
            drawBlock(posChecks[0].pos, backColor);
            posChecks[0].isUse = false;
            drawBlock(posChecks[1].pos, backColor);
            posChecks[1].isUse = false;
            if(posChecks[2].isUse == true)
            {
                drawBlock(posChecks[2].pos, backColor);
            }
            var zhongxin = (posChecks[0].pos + posChecks[1].pos) / 2;
            posChecks[2].pos = zhongxin;
            posChecks[2].isUse = true;
            drawBlock(zhongxin, new Color(0.75f, 0.3f, 0f, 1f));
            setZhongXinValue(zhongxin);
            return;
        }
        else
        {
            drawBlock(tempPos.pos, new Color(0.25f, 0.6f, 0, 1f));
            posChecks[i].pos = tempPos.pos;
            posChecks[i].isUse = true;
        }

    }
    void onDoubleTouchHandler(float nowTouchDistance)
    {
        var nowOffset = (nowTouchDistance - OldTouchCurrDis) * 0.025f;
        OldTouchCurrDis = nowTouchDistance;
        RectTransform rectTransform = transform.Find("Image/test") as RectTransform;
        rectTransform.localScale = rectTransform.localScale + (new Vector3(nowOffset,nowOffset));
    }
    private void LateUpdate()
    {
        if (utilsTools.IsPhone)
        {
            if (Input.touchCount == 2)
            {
                Touch touch1 = Input.GetTouch(0);
                Touch touch2 = Input.GetTouch(1);
                if (
                (touch1.phase == TouchPhase.Began && (touch2.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Stationary)) ||
                (touch2.phase == TouchPhase.Began && (touch1.phase == TouchPhase.Moved || touch1.phase == TouchPhase.Stationary))
                ){
                    Debug.Log("double Touch Begin");
                    
                    Vector2 touchPos1 = touch1.position;
                    Vector2 touchPos2 = touch2.position;
                    
                    mainGameView mainView = GetComponent<mainGameView>();
                    var ImageTransform = image.transform as RectTransform;
                    Vector2 vector1 = utilsTools.ScreenToUILocalPos(ImageTransform, touchPos1);
                    Vector2 vector2 = utilsTools.ScreenToUILocalPos(ImageTransform, touchPos2);
                    if(ImageTransform.rect.Contains(vector1) && ImageTransform.rect.Contains(vector2) )
                    {
                        Vector2 vector3 = (vector1 + vector2) / 2;
                        for (int i = 0; i < 3; i++)
                        {
                            if (posChecks[i].isUse)
                            {
                                drawBlock(posChecks[i].pos, backColor);
                            }
                            posChecks[i].isUse = true;
                        }
                        drawBlock(vector1, new Color(0.25f, 0.6f, 0, 1f));
                        drawBlock(vector2, new Color(0.25f, 0.6f, 0, 1f));
                        drawBlock(vector3, new Color(0.65f, 0.2f, 0.3f, 1f));
                        posChecks[0].pos = vector1;
                        posChecks[1].pos = vector2;
                        posChecks[2].pos = vector3;
                        setZhongXinValue(vector3);
                        OldTouchCurrDis = Vector2.Distance(vector1, vector2);
                    }
                    else
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            if (posChecks[i].isUse)
                            {
                                drawBlock(posChecks[i].pos, backColor);
                            }
                            posChecks[i].isUse = false;
                        }
                    }
                    return;
                }
                if (
                    (touch1.phase == TouchPhase.Moved && (touch2.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Stationary)) ||
                    (touch2.phase == TouchPhase.Moved && (touch1.phase == TouchPhase.Moved || touch1.phase == TouchPhase.Stationary))
                 )
                {
                    if (posChecks[0].isUse == true)
                    {
                        var ImageTransform = image.transform as RectTransform;
                        Vector2 vector1 = utilsTools.ScreenToUILocalPos(ImageTransform, touch1.position);
                        Vector2 vector2 = utilsTools.ScreenToUILocalPos(ImageTransform, touch2.position);
                        onDoubleTouchHandler(Vector2.Distance(vector1, vector2));
                    }
                    return;
                }
                if (touch1.phase == TouchPhase.Canceled || touch1.phase == TouchPhase.Ended || touch2.phase == TouchPhase.Canceled || touch2.phase == TouchPhase.Ended)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (posChecks[i].isUse)
                        {
                            drawBlock(posChecks[i].pos, backColor);
                        }
                        posChecks[i].isUse = false;
                    }
                    //if (OldTouchCurrDis > 999f)
                    //{
                    //    Debug.Log("double Touch End");
                    //    setViewAnchor(new Vector2(0.5f, 0.5f));
                    //    skipDoubleScale = false;
                    //    OldTouchCurrDis = -1000;
                    //}
                }
            }
        }
        else
        {

            if (Input.GetMouseButtonDown(0))
            {
                this.tempPos = new PosChecks();
                tempPos.pos = Input.mousePosition;
                tempPos.isUse = true;
            }
            else
            {
                if (tempPos != null && tempPos.isUse)
                {
                    tempPos.isUse = false;
                    var rectTransform = transform.Find("Image").GetComponent<RectTransform>();
                    Vector2 vector = utilsTools.ScreenToUILocalPos(rectTransform, tempPos.pos);
                    if (rectTransform.rect.Contains(vector))
                    {
                        Debug.Log("在区域内");
                        tempPos.pos = vector;
                        pushDrawPos();
                    }
                    else
                    {
                        Debug.Log("不在区域内");
                    }
                }
            }
        }
    }
    private void Update()
    {
        ActionManages.update();
        if (colorIsChange)
        {
            colorIsChange = false;
            image.sprite.texture.SetPixels(colors);
            image.sprite.texture.Apply();
        }
    }
    
}
