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


public class gameEndLogic : MonoBehaviour,IEventHandler
{
    // Start is called before the first frame update
    public GameObject gameShowView = null;
    public GameObject saoguangObj = null;
    private GameScenUserInfo gameScenUseInfo = null;
    private short nowPlayerId = -2;
    public int replayerTimeInterval = 25;
    public Rect rect = new Rect();
    public int frameRate = 30;
    private int mp4OperNext = -1;
    void Start()
    {
        
    }
    public void initMember()
    {
        if(utilsTools.ContainsName(this, "inSaveProgress") == false)
        {
            transform.Find("inSaveProgress").gameObject.SetActive(false);
            transform.Find("gameShowBase").gameObject.SetActive(false);
            utilsTools.onEvent(this, "initViewShowData");
            utilsTools.onEvent(this, "onClick_saveOptions_msg");
        }
    }
    public void onShareHandler()
    {
        GameObject gobj = transform.Find("saveOptions").gameObject;
        float x = transform.Find("shareBtn").localPosition.x;
        gobj.transform.localPosition = new Vector2(x,gobj.transform.localPosition.y);
        gobj.SetActive(true);
        mp4OperNext = 1;
    }
    public void onSaveHandler()
    {
        GameObject gobj = transform.Find("saveOptions").gameObject;
        float x = transform.Find("saveBtn").localPosition.x;
        gobj.transform.localPosition = new Vector2(x, gobj.transform.localPosition.y);
        gobj.SetActive(true);
        mp4OperNext = 2;
    }
    public void onRePalyHandler(bool isFrist = false)
    {
        //清空所有颜色
        mainGameView gameView = gameShowView.GetComponent<mainGameView>();
        utilsTools.delTimeHandler(gameObject, "backPaly");
        if(nowPlayerId > -1)
        {
            gameView.showArea(-1);
            nowPlayerId = -2;
        }
        else
        {            
            gameView.reLoadLineTexture((bool isSuccess) =>
            {
                if (isSuccess)
                {
                    nowPlayerId = -1;
                    if(replayerTimeInterval < 4)
                    {
                        replayerTimeInterval = 4;
                    }
                    if(replayerTimeInterval > 60) {
                        replayerTimeInterval = 60;
                    }
                    utilsTools.addTimer_Update(gameObject, "backPaly", (string name) =>
                    {
                        if (nowPlayerId == -2) { return false; }
                        else
                        {
                            nowPlayerId++;
                            if (gameScenUseInfo.areaInfoUseData.Count <= nowPlayerId) return false;
                            int aid = gameScenUseInfo.areaInfoUseData[nowPlayerId].id;
                            gameView.showArea(aid);
                            return true;
                        }
                    }, Mathf.Abs(replayerTimeInterval), gameScenUseInfo.areaInfoUseData.Count);
                }
                else
                {
                    Debug.Log("出现未知原因无法加载");
                }
            });
        }
    }
    public void onContinueHandler()
    {
        utilsTools.sendEventObjectParams("closeGameScene");
    }
    //保存成mp4
    private void onSaveToMp4Handler()
    {
        transform.Find("palyer").gameObject.SetActive(false);
        transform.Find("shareBtn").gameObject.SetActive(false);
        transform.Find("saveBtn").gameObject.SetActive(false);
        transform.Find("saveOptions").gameObject.SetActive(false);
        transform.Find("Continue").gameObject.SetActive(false);
        transform.Find("inSaveProgress").gameObject.SetActive(true);
        mainGameView gameView = gameShowView.GetComponent<mainGameView>();
        utilsTools.delTimeHandler(gameObject, "backPaly");
        nowPlayerId = -2;
        gameView.reLoadLineTexture((bool isSuccess) =>
        {
            if(isSuccess)
            {
                Texture2D lineTexture = gameView.lineImg.GetComponent<Image>().sprite.texture;
                int idxflg = 0;
                float size = (float)gameScenUseInfo.areaInfoUseData.Count;
                utilsTools.addTimer_Update(gameObject, "saveToMp4", (string name) =>
                {
                    int aid = gameScenUseInfo.areaInfoUseData[idxflg].id;
                    float jindu = idxflg / size;
                    idxflg++;
                    gameView.showArea(aid,null);
                    gameObject.transform.Find("inSaveProgress/progress").GetComponent<Image>().fillAmount = jindu;
                    if(idxflg == gameScenUseInfo.areaInfoUseData.Count)
                    {
                        onCloseMp4Save(true);
                    }
                    return true;
                }, 5, gameScenUseInfo.areaInfoUseData.Count);
            }
        });
    }
    public void onCloseMp4Save(bool isSuccesss = false) {
        transform.Find("palyer").gameObject.SetActive(true);
        transform.Find("shareBtn").gameObject.SetActive(true);
        transform.Find("saveBtn").gameObject.SetActive(true);
        transform.Find("saveOptions").gameObject.SetActive(true);
        transform.Find("Continue").gameObject.SetActive(true);
        transform.Find("inSaveProgress").gameObject.SetActive(false);
        if(isSuccesss == false)
        {
            mp4OperNext = -1;
        }
        else
        {
            if(mp4OperNext == 1)
            {
                Debug.Log("分享中");
            }
            else
            {
                Debug.Log("保存完成");
            }
        }
    }

    public void initView()
    {
        nowPlayerId = -1;
        transform.Find("inSaveProgress").gameObject.SetActive(false);
        transform.Find("saveOptions").gameObject.SetActive(false);

        GameObject gameShowBase = transform.Find("gameShowBase").gameObject;
        Image image = gameShowBase.GetComponent<Image>();
        Texture2D texture = image.sprite.texture;
        //Color[] colors = texture.GetPixels(0, 0, texture.width, texture.height);
        //int cout = utilsTools.checkResetYuanJiao(colors, texture.width, texture.height, 65);
        //if (cout > 0)
        //{
        //    texture.SetPixels(0, 0, texture.width, texture.height, colors);
        //    texture.Apply();
        //}
        gameShowBase.SetActive(false);
        RectTransform rectTransform = gameShowBase.GetComponent<RectTransform>();
        RectTransform rectTransform1 = gameShowView.GetComponent<RectTransform>();
        var offsetPos = Vector2.zero;
        Vector3 rightTopPos = rectTransform.TransformPoint(new Vector3(rectTransform.rect.xMax, rectTransform.rect.yMax));
        Vector2 objWordPos = new Vector2(rightTopPos.x - offsetPos.x, rightTopPos.y - offsetPos.y);
        Vector2 objScale = new Vector2();
        objScale.x = rectTransform.rect.width * rectTransform.localScale.x / rectTransform1.rect.width;
        objScale.y = rectTransform.rect.height * rectTransform.localScale.y / rectTransform1.rect.height;
        ActionManages.addMoveByRelativePos(gameShowView, new Vector2(0,324), 2);
        Debug.Log("收起");
        ActionManages.addMoveByScale(gameShowView,objScale, 0.35f,(GameObject gobj,int aid) => {
            gameShowView.transform.SetParent(gameShowBase.transform);
            var saoguang = Instantiate(saoguangObj);
            saoguang.transform.SetParent(gameShowBase.transform);
            RectTransform rectTransform2 = saoguang.GetComponent<RectTransform>();
            var bet = rectTransform.rect.height / rectTransform2.rect.height;
            rectTransform2.sizeDelta = new Vector2(rectTransform2.rect.width, rectTransform.rect.height);
            rectTransform2.localPosition = new Vector2(0 - rectTransform2.rect.width / 2 - rectTransform.rect.width / 2, 0);
            Debug.Log(string.Format("x = {0},y = {1},width = {2},height={3}", rectTransform2.localPosition.x, rectTransform2.localPosition.y, rectTransform2.sizeDelta.x, rectTransform2.sizeDelta.y));

            //addRoundedRectangle(gameShowView.transform.Find("up").gameObject);
            //addRoundedRectangle(gameShowView.transform.Find("low").gameObject);
            //addRoundedRectangle(gameShowView.transform.Find("Image").gameObject);
            gameObject.SetActive(true);
            gameShowBase.SetActive(true);
            Debug.Log("扫光");
            ActionManages.addMoveByRelativePos(saoguang, new Vector2(rectTransform.rect.width + rectTransform2.rect.width, 0), 0.35f, (GameObject gobj, int aid) => {
                Destroy(gobj);
                //翻转
                Debug.Log("缩窄");
                ActionManages.addMoveByScale(gameShowView, new Vector2(0.1f, objScale.y),0.25f, (GameObject gobj, int aid) => {
                    gameShowBase.transform.localScale = new Vector2(0.1f, 1);
                    gameShowView.transform.localScale = objScale;
                    Debug.Log("变宽");
                    ActionManages.addMoveByScale(gameShowBase, new Vector2(1f, 1f),0.25f, (GameObject gobj, int aid) => {
                        onRePalyHandler(true);
                    });
                });
            });
        });
    }

    public void onEventHandler(string eventName, List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        if (eventName.Equals("initViewShowData"))
        {
            this.gameScenUseInfo = (GameScenUserInfo)objects[0];
        }else if (eventName.Equals("onClick_saveOptions_msg"))
        {
            int typeId = (int)objects[0];
            if (typeId.Equals(0))
            {
                transform.Find("saveOptions").gameObject.SetActive(false);
            }else if (typeId.Equals(1))
            {
            }
            else if (typeId.Equals(2))
            {
                onSaveToMp4Handler();
            }
        }
    }
    
}
