﻿#if !NETFX_CORE
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using UnityEngine.SceneManagement;
using I2.Loc;
using UnityEngine.UI;
using System;
#pragma warning disable 618


public partial class I2Loc_PlayTest_Features
{
    [UnityTest, Category("Features")]
    public IEnumerator I2LocTest_UnloadLanguages()
    {
        SceneManager.LoadScene("I2Localization    UnityUI");
        yield return null;
        LocalizationManager.CurrentLanguage = "English";

        Assert.IsTrue(LocalizationManager.Sources.Count == (LocalizationManager.GlobalSources.Length + 1));

        var source = I2Utils.FindObject("LocalizationSource").GetComponent<LanguageSource>().mSource;
        var globalSource = LocalizationManager.Sources[0];

        var languageData = source.GetLanguageData("French");
        languageData.SetCanBeUnLoaded(false);
        source.LoadAllLanguages();
        source._AllowUnloadingLanguages = LanguageSourceData.eAllowUnloadLanguages.EditorAndDevice;
        source.SaveLanguages(true);

        var localTermData = source.GetTermData("Option A");
        source.LoadAllLanguages();

        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(0)));
        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(1)));
        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(2)));

        I2Utils.FindObject("Canvas/Buttons/Spanish").GetComponent<Button>().onClick.Invoke();
        yield return null;

        Assert.IsTrue(localTermData.GetTranslation(0)==null);
        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(1)));
        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(2)));
        for (int i = 0; i < globalSource.mLanguages.Count; ++i)
        {
            if (globalSource.IsCurrentLanguage(i) || !globalSource.mLanguages[i].CanBeUnloaded())
                Assert.IsTrue(globalSource.mLanguages[i].IsLoaded());
            else
                Assert.IsFalse(globalSource.mLanguages[i].IsLoaded());
        }

        I2Utils.FindObject("Canvas/Buttons/English").GetComponent<Button>().onClick.Invoke();
        yield return null;

        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(0)));
        Assert.IsTrue(localTermData.GetTranslation(1) == null);
        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(2)));
        for (int i = 0; i < globalSource.mLanguages.Count; ++i)
        {
            if (globalSource.IsCurrentLanguage(i) || !globalSource.mLanguages[i].CanBeUnloaded())
                Assert.IsTrue(globalSource.mLanguages[i].IsLoaded());
            else
                Assert.IsFalse(globalSource.mLanguages[i].IsLoaded());
        }

        I2Utils.FindObject("Canvas/Buttons/French").GetComponent<Button>().onClick.Invoke();
        yield return null;

        Assert.IsTrue(localTermData.GetTranslation(0) == null);
        Assert.IsTrue(localTermData.GetTranslation(1) == null);
        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(2)));
        for (int i = 0; i < globalSource.mLanguages.Count; ++i)
        {
            if (globalSource.IsCurrentLanguage(i) || !globalSource.mLanguages[i].CanBeUnloaded())
                Assert.IsTrue(globalSource.mLanguages[i].IsLoaded());
            else
                Assert.IsFalse(globalSource.mLanguages[i].IsLoaded());
        }

        source.LoadAllLanguages();
        source._AllowUnloadingLanguages = LanguageSourceData.eAllowUnloadLanguages.Never;
        source.SaveLanguages(true);
        yield return null;

        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(0)));
        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(1)));
        Assert.IsTrue(!string.IsNullOrEmpty(localTermData.GetTranslation(2)));
    }
}

#endif