using SUIFW;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class AdvancedDrawingPad : MonoBehaviour
{
    private GameObject clone;
    private LineRenderer line;
    private int i;
    private int index = 0;
    public static Material mat;
    public static bool canUse;
    public GameObject obs;
    public GameObject lineparent;
    private Vector3 startpos;
    public RectTransform canvas;
    public GameObject listColor;
    public Sprite[] spriteList;
    public Image img;
    int colorIndex = 1;
    public static AdvancedDrawingPad Instance;
    void Start()
    {
        Instance = this;
    }
    void Awake()
    {
        
    }
    public static bool isqianming;
    private void OnEnable()
    {
        isqianming=false;
        // 强制横屏并禁止
        // 自动旋转
            Screen.orientation = ScreenOrientation.LandscapeLeft;
            Screen.autorotateToLandscapeLeft = false;
            Screen.autorotateToLandscapeRight = false;
            Screen.autorotateToPortrait = false;
            Screen.autorotateToPortraitUpsideDown = false;
        canUse = true;
        listColor.SetActive(false);
        colorIndex = 1;
    }

    public void showAndCloseColor()
    {
        listColor.SetActive(!listColor.activeSelf);
        canUse = !listColor.activeSelf;
    }
    public static Action<Texture2D> callback;
    public void closejietu()
    {
        if(isqianming)
        {
            Rect rect = new Rect(0, 0, Screen.width, Screen.height);
            callback.Invoke(ScreenShot(Camera.main,rect));
        }
    }
    // <summary>
    /// 传入一个 RectTransform 区域!, 就不把资格区域的图像保存下来
    /// </summary>
    /// <param name="rect"></param>
    public Texture2D ScreenShot(Camera camera, Rect rect)
    {
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        camera.targetTexture = rt;
        camera.Render();
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 125);
        screenShot.Apply();
        camera.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(rt);
        //Debug.Log("截图成功");
        return screenShot;
       
    }
    void Update()
    {
        if (mat == null)
        {
            mat = new Material(Shader.Find("Shader/line"));
        }
        if (canUse)
        {
            if (Input.GetMouseButtonDown(0))
            {
                clone = (GameObject)Instantiate(obs, new Vector3(0, 0, this.transform.position.z), this.transform.rotation, lineparent.transform);//克隆一个带有LineRender的物体           
                clone.name += index.ToString();
                line = clone.AddComponent<LineRenderer>();//获得该物体上的LineRender组件  
                //line.material = new Material(Shader.Find("Shader/Additive"));
                line.material = new Material(Shader.Find("Sprites/Default"));
                switch(colorIndex)
                {
                    case 1:
                        mat.color = Color.black;
                        break;
                    case 2:
                        mat.color = Color.black;
                        break;
                    case 3:
                        mat.color = Color.black;
                        break;
                    case 4:
                        mat.color = Color.black;
                        break;
                    default:
                        mat.color = Color.black;
                        break;
                }
                line.sortingOrder = 1;
                //设置颜色
                line.startColor = mat.color;
                line.endColor = mat.color;
                //设置宽度  
                line.startWidth = 0.5f;
                line.endWidth = 0.5f;
                line.useWorldSpace = false;
                i = 0;
                index++;
                startpos = GetuiPos();
            }
            if (Input.GetMouseButton(0))
            {
                if (line != null)
                {
                    if (Input.mousePosition.x < 0 || Input.mousePosition.x > Screen.width || Input.mousePosition.y < 0 || Input.mousePosition.y > Screen.height)
                    {
                    }
                    listColor.SetActive(false);

                    Vector3 pos = GetuiPos();
                    if (Vector3.Distance(startpos, pos) > 0.5f)
                    {
                        i++;
                        line.positionCount = i;
                        line.SetPosition(i - 1, pos);//设置顶点位置 s
                    }
                    isqianming=true;
                }
            }
            if (Input.GetMouseButtonUp(0))
            {
                line = null;
                
            }
        }
    }
    public Vector2 GetuiPos()
    {
        Vector2 uisize = canvas.sizeDelta;//得到画布的尺寸
        Vector2 screenpos = Input.mousePosition;
        Vector2 screenpos2;
        screenpos2.x = screenpos.x - (Screen.width / 2);//转换为以屏幕中心为原点的屏幕坐标
        screenpos2.y = screenpos.y - (Screen.height / 2);
        Vector2 uipos;//UI坐标
        uipos.x = screenpos2.x * (uisize.x / Screen.width);//转换后的屏幕坐标*画布与屏幕宽高比
        uipos.y = screenpos2.y * (uisize.y / Screen.height);
        return uipos;
    }
 
    public void ClickPaint(int ci)
    {
        listColor.SetActive(false);
        colorIndex = ci;
        canUse = true;
        img.sprite = spriteList[ci - 1];
    }
    public void DeleteLine()
    {
        if (lineparent.transform.childCount > 0)
        {
            for (int i = 0; i < lineparent.transform.childCount; i++)
            {
                Destroy(lineparent.transform.GetChild(i).gameObject);
            }
        }
        line = null;
    }
    private void OnDisable()
    {
            // 强制横屏并禁止自动旋转
            Screen.orientation = ScreenOrientation.Portrait;
            Screen.autorotateToLandscapeLeft = false;
            Screen.autorotateToLandscapeRight = false;
            Screen.autorotateToPortrait = false;
            Screen.autorotateToPortraitUpsideDown = false;
        if (lineparent.transform.childCount > 0)
        {
            for (int i = 0; i < lineparent.transform.childCount; i++)
            {
                Destroy(lineparent.transform.GetChild(i).gameObject);
            }
        }
    }
}