/*
Creat By LuoJiasi;
Plz Put in DIY File;
*/
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using NUnit.Framework;
using Thousandto.Core.Base;
using Thousandto.Code.Center;
using System.Linq;
using CoroutinePool = UnityEngine.Gonbest.MagicCube.CoroutinePool;

namespace Funcell.DIY.UniScene
{
    public class RecordTest : EditorWindow
    {
		[MenuItem("Ares/测试工具/录制操作")]
        static void Init()
        {
            var win = (RecordTest)EditorWindow.GetWindow(typeof(RecordTest));
            win.Show();
			win.titleContent = new GUIContent("录制操作");
        }
		#region 配置文件
		protected string _now = DateTime.Now.ToString ("yyyy-MM-dd/") + DateTime.Now.ToString ("HH:mm:ss");
		protected string _logplace = "D:/TestCase/RecordResult.txt" ; //Log存储位置
		private int recordStatus = 0;

		public List<GameObject> btnlist = new List<GameObject>(); 
		private List<GameObject> allbtnlist = new List<GameObject>(); 
				
		//测试结果记录到txt文件中,文件地址为_logplace
		protected void LogTest(string resultfile)
		{
			if (!File.Exists(_logplace))
			{
				File.WriteAllText(_logplace, "[Record Start]\r\n");
			}
			File.AppendAllText(_logplace,resultfile + "\r\n");
			Debug.LogWarning(resultfile);
		}
		#endregion

		private void OnGUI()
        {
			if(GUILayout.Button("录制操作"))
			{
				recordStatus = 0;
				LogTest("【开始录制】");
				CoroutinePool.AddTask(AutoRecord());
			}
			
			if(GUILayout.Button("停止录制"))
			{
				recordStatus = 1;
			}
		}
		private IEnumerator AutoRecord()
		{
            while (recordStatus == 0)
            {
				GetBtn();
                List<GameObject> reslut = btnlist.Except(allbtnlist).ToList();
                if (btnlist == null)
                {
                    yield return new WaitForSeconds(0.1f);
                }
				else if(reslut == null)
				{
					yield return new WaitForSeconds(0.1f);	
				}
				else
				{
					allbtnlist.AddRange(reslut);
					for (int i = 0; i < reslut.Count; i++)
					{
						UIEventListener.Get (reslut[i]).onClick += OnClick_Record;
					}			
					reslut.Clear();
				}					
				yield return new WaitForSeconds(0.1f);				
				if (recordStatus == 1)
				{
					LogTest("【停止录制】");
					yield break;
				}
			}
		}

		public void GetBtn()
		{
			GameObject go = GameObject.Find ("UIRoot");	
			btnlist.Clear ();
			foreach (UIButton obj in go.transform.GetComponentsInChildren<UIButton>())
			{
				btnlist.Add(obj.gameObject);
			}
		}
		private void OnClick_Record(GameObject go)
		{
            UILabel label = go.transform.GetComponentInChildren<UILabel>();
            if (label)
            {
                LogTest("ClickBtn," + GetBtnFullName(go) + "," + label.text + ",");
            }
            else
            {
                LogTest("ClickBtn," + GetBtnFullName(go) + ",");
            }
		}
		private string GetBtnFullName(GameObject obj)
        {
            string fullname = obj.name;
            GameObject pa_obj = obj.transform.parent.gameObject;
            for (int i = 0; i < 10; i++)
            {
                if (pa_obj.name.Contains("UIRoot"))
                {
                    break;
                }
                if (pa_obj.name.Contains("[Chain]"))
                {
                    break;
                }
                fullname = pa_obj.name + "/" + fullname;
                pa_obj = pa_obj.transform.parent.gameObject;
            }
            return fullname;
        }	
    }
}