﻿using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Linq;
using I2.Loc;

public partial class I2LocTest_EditMode_Basics
{
    Assembly mAssemblyEditor;
    System.Type mTypeInspectorWindow;
    List<Object> mObjectsToCleanup = new List<Object>();

    [SetUp]
    public void Initialize()
    {
        if (mAssemblyEditor != null)
            return;

        mAssemblyEditor = Assembly.GetAssembly(typeof(Editor));
        mTypeInspectorWindow = mAssemblyEditor.GetType("UnityEditor.InspectorWindow");
    }

    [TearDown]
    public void Cleanup()
    {
        foreach (var obj in mObjectsToCleanup)
            Object.DestroyImmediate(obj);
        mObjectsToCleanup.Clear();
    }

    IEnumerator RepaintEditor()
    {
        mTypeInspectorWindow.GetMethod("RepaintAllInspectors", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null);
        //EditorUtility.SetDirty(LocalizationEditor.mLanguageSource);
        return null;
    }

    IEnumerable WWWfinished()
    {
        yield return RepaintEditor();
        yield return null;
        while (LocalizationEditor.mLanguageSourceEditor.mConnection_WWW != null)
            yield return null;
    }


    [UnityTest, Category("Editor")]
    public IEnumerator Test_LanguageSource_Edit()
    {
        var sourceObj = new GameObject("Source").AddComponent<LanguageSource>();
        var source = sourceObj.mSource;
        mObjectsToCleanup.Add(sourceObj.gameObject);
        Selection.activeObject = sourceObj;

        var pText = new GameObject("Text", typeof(TextMesh), typeof(I2.Loc.Localize)).GetComponent<TextMesh>();
        pText.text = "TestCategory/My<\\$._Term3";
        mObjectsToCleanup.Add(pText.gameObject);

        yield return RepaintEditor();

        #region Create Languages

        LocalizationEditor.mCurrentViewMode = LocalizationEditor.eViewMode.Languages;
        LocalizationEditor.mLanguages_NewLanguage = "eng ca";
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_AddLanguageFromPopup;
        yield return RepaintEditor();

        LocalizationEditor.mLanguages_NewLanguage = "span";
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_AddLanguageFromPopup;
        yield return RepaintEditor();

        LocalizationEditor.mLanguages_NewLanguage = "ara";
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_AddLanguageManual;
        yield return RepaintEditor();


        Assert.IsTrue(source.mLanguages.Count == 3);
        Assert.IsTrue(source.mLanguages[0].Name == "English (Canada)");
        Assert.IsTrue(source.mLanguages[0].Code == "en-CA");
        Assert.IsTrue(source.mLanguages[1].Name == "Spanish");
        Assert.IsTrue(source.mLanguages[1].Code == "es");
        Assert.IsTrue(source.mLanguages[2].Name == "ara");
        Assert.IsTrue(source.mLanguages[2].Code == "ar");

        #endregion

        #region Create Terms

        LocalizationEditor.mCurrentViewMode = LocalizationEditor.eViewMode.Keys;
        bool hasMissingEnabled = (LocalizationEditor.mFlagsViewKeys & (int)LocalizationEditor.eFlagsViewKeys.Missing)>0;
        if (!hasMissingEnabled)
            LocalizationEditor.mFlagsViewKeys |= (int)LocalizationEditor.eFlagsViewKeys.Missing;

        LocalizationEditor.mTermsList_NewTerm = "Term1";
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_AddTerm_InTermsList;
        yield return RepaintEditor();

        LocalizationEditor.mTermsList_NewTerm = "TestCategory/SubCategory/Term2";
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_AddTerm_InTermsList;
        yield return RepaintEditor();

        LocalizationEditor.mTermsList_NewTerm = "TestCategory/Term2";
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_AddTerm_InTermsList;
        yield return RepaintEditor();

        LocalizationEditor.mTermsList_NewTerm = "TestCategory/Term11";
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_AddTerm_InTermsList;
        yield return RepaintEditor();


        Assert.IsTrue(LocalizationEditor.mParsedTerms.ContainsKey("TestCategory My< $._Term3"));
        Assert.IsTrue(LocalizationEditor.mParsedCategories.Contains("TestCategory"));
        Assert.IsTrue(!LocalizationEditor.mSelectedKeys.Contains("TestCategory My< $._Term3"));

        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_SelectTerms_Missing;
        yield return RepaintEditor();
        Assert.IsTrue(LocalizationEditor.mSelectedKeys.Contains("TestCategory My< $._Term3"));

        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_AddSelectedTerms;
        yield return RepaintEditor();

        Assert.IsTrue(source.mTerms.Count >= 3);
        Assert.IsTrue(source.GetTermData("Term1") != null);
        Assert.IsTrue(source.GetCategories().Count>=3);
        Assert.IsTrue(source.GetTermsList("TestCategory").Count == 2);
        Assert.IsTrue(source.GetTermsList("TestCategory/SubCategory").Count == 1);
        Assert.IsTrue(source.GetTermData("TestCategory My< $._Term3") !=null);

        if (!hasMissingEnabled)
            LocalizationEditor.mFlagsViewKeys &= ~(int)LocalizationEditor.eFlagsViewKeys.Missing;

        #endregion

        #region Remove Terms

        LocalizationEditor.mSelectedKeys.Clear();
        LocalizationEditor.mSelectedKeys.Add("TestCategory My< $._Term3");
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_RemoveSelectedTerms;
        yield return RepaintEditor();
        Assert.IsTrue(source.GetTermData("TestCategory/My< $._Term3") == null);

        LocalizationEditor.mKeyToExplore = "TestCategory/Term2";
        LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.Button_DeleteTerm;
        yield return RepaintEditor();
        yield return null;
        Assert.IsTrue(source.GetTermData("TestCategory/Term2") == null);
        Assert.IsTrue(source.GetTermsList("TestCategory").Count == 0);
        Assert.IsTrue(source.GetTermsList("TestCategory/SubCategory").Count == 1);

        #endregion

    }

}
