using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEditor;
using System.IO;
using GCGame.Table;
using System.Collections.Generic;

[ExecuteInEditMode]
public class GetMapDataEditor : EditorWindow
{
    public static string FixDataSavePath = "Assets/Project3D/BundleData/Other/";
    public static string PNGSavePath = "Project3D/BundleData/UI/Texture/worldMap/";

    Camera m_Camera;

    [MenuItem("ProTool/SceneMapFix/GetSceneMapFixWnd")]
    static void Init()
    {
        GetMapDataEditor window = (GetMapDataEditor)EditorWindow.GetWindow(typeof(GetMapDataEditor));
        window.minSize = new Vector2(200, 200);
        window.Show();
    }
    Dictionary<T4MObjSC, int> T4MObjSCs = new Dictionary<T4MObjSC, int>();
    void Awake()
    {
        m_Camera = new GameObject().AddComponent<Camera>();
        if (m_Camera == null)
            Close();
        
        m_Camera.orthographic = true;
        Selection.activeGameObject = m_Camera.gameObject;

        T4MObjSC[] t4ms = GameObject.FindObjectsOfType<T4MObjSC>();
        for(int i=0;i<t4ms.Length;i++)
        {
            if(t4ms[i]!=null)
            {
                T4MObjSCs[t4ms[i]] = t4ms[i].Master;
                t4ms[i].Master = 0;
            }
        }
    }

    void OnDestroy()
    {
        if(m_Camera!=null)
        {
            GameObject.DestroyImmediate(m_Camera.gameObject);
        }
        foreach(var value in T4MObjSCs)
        {
            if(value.Key!=null)
            {
                value.Key.Master = value.Value;
            }
        }
    }

    string tips = "";

    void ShowTips(string tip)
    {
        tips = tip;
        Debug.Log(tips);
    }

    void OnGUI()
    {
        if (GUILayout.Button("SetSceneSize"))
        {
            SceneView.lastActiveSceneView.minSize = new Vector2(m_Camera.pixelWidth, m_Camera.pixelHeight);
            SceneView.lastActiveSceneView.maxSize = new Vector2(m_Camera.pixelWidth, m_Camera.pixelHeight);
        }
        if (GUILayout.Button("SaveCurrSceneFixData"))
        {
            string sceneName = SceneManager.GetActiveScene().name;
            sceneName = sceneName.ToLower();
            string PNGPath = Application.dataPath + "/" + PNGSavePath + sceneName + ".jpg";
            ScreenCapture.CaptureScreenshot(PNGPath, 0);
            //CaptureScreen(PNGPath);
            Vector2 sizeRect = new Vector2(m_Camera.orthographicSize * 2 * m_Camera.aspect, m_Camera.orthographicSize * 2);

            SceneMapFixData newFixData = ScriptableObject.CreateInstance<SceneMapFixData>();
            newFixData.SceneName = SceneManager.GetActiveScene().name;
            newFixData.m_Pos = m_Camera.transform.localPosition;
            newFixData.m_EulerAngles = m_Camera.transform.localEulerAngles;
            newFixData.m_SizeRect = sizeRect;
            AssetDatabase.CreateAsset(newFixData, FixDataSavePath + sceneName + ".asset");
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            var prefabImporter = AssetImporter.GetAtPath(FixDataSavePath + sceneName + ".asset");
            if (prefabImporter != null)
            {
                prefabImporter.assetBundleName = "other/" + sceneName;
                prefabImporter.assetBundleVariant = "common";
            }

            var prefabPngImporter = AssetImporter.GetAtPath("Assets/"+ PNGSavePath + sceneName + ".jpg");
            if (prefabPngImporter != null)
            {
                prefabPngImporter.assetBundleName = "ui/texture/worldmap/" + sceneName;
                prefabPngImporter.assetBundleVariant = "common";
            }

            ShowTips("Save Curr Scene FixData succ ...");
        }
        if (GUILayout.Button("ShowCurSceneMapData"))
        {
            SceneMapFixData fixData = AssetDatabase.LoadMainAssetAtPath(FixDataSavePath + SceneManager.GetActiveScene().name + ".asset") as SceneMapFixData;
            if (fixData == null)
            {
                ShowTips("Fix Data is null...");
                return;
            }
            ShowTips(string.Format("Cur Pos = {0}  EulerAngles = {1}  Size = {2}", fixData.m_Pos, fixData.m_EulerAngles, fixData.m_SizeRect));
        }

        GUILayout.TextArea(tips);
    }

    void CaptureScreen(string path)
    {
        Texture2D screenTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
        // put buffer into texture
        screenTexture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height), 0, 0);
        screenTexture.Apply();

        byte[] dataToSave = screenTexture.EncodeToJPG();
        File.WriteAllBytes(path, dataToSave);
    }

    void Update()
    {
        if (Selection.activeGameObject == null)
            return;
        Camera camera = Selection.activeGameObject.GetComponent<Camera>();
        if (camera == null || camera!=m_Camera)
            return;
        if (SceneView.lastActiveSceneView == null)
            return;
        SceneView.lastActiveSceneView.orthographic = true;
        camera.orthographicSize = SceneView.lastActiveSceneView.size / 2;
        SceneView.lastActiveSceneView.AlignWithView();

        //if (Input.GetKeyDown(KeyCode.C))
        //{
        //    string sceneName = SceneManager.GetActiveScene().name;
        //    sceneName = sceneName.ToLower();
        //    string PNGPath = Application.dataPath + "/" + PNGSavePath + sceneName + ".jpg";
        //    //Application.CaptureScreenshot(PNGPath, 0);
        //    CaptureScreen(PNGPath);
        //}
    }
}