import FairyGUI = CS.FairyGUI;
import FairyEditor = CS.FairyEditor;
import UnityEngine = CS.UnityEngine;
const App = FairyEditor.App;
import languageSetting from './LanguageSettings'



class MembersLanguageCustomInspector extends FairyEditor.View.PluginInspector {
    private setUseKey: FairyGUI.GButton;
    private btnRefresh: FairyGUI.GButton;
    private languageKey: FairyEditor.Component.TextInput;

    public constructor() {
        super();

        this.panel = FairyGUI.UIPackage.CreateObject("WhootCustomInspector", "LanguageComponent").asCom;
        this.setUseKey = this.panel.GetChild("SetUseKey").asButton;
        this.btnRefresh = this.panel.GetChild("btnRefresh").asButton;
        this.languageKey = this.panel.GetChild("languageKey") as FairyEditor.Component.TextInput

        this.btnRefresh.onClick.Add(() => {
            let sels = App.activeDoc.inspectingTargets
            let obj = sels.get_Item(0);
            languageSetting.updateItemTitle(obj, this.languageKey.title)
        })
        this.setUseKey.onChanged.Add(() => {
            this.updateData()
        });

        this.languageKey.onFocusOut.Add(() => {
            this.updateData()
        })


        this.updateAction = () => { return this.updateUI(); };
    }

    private updateData() {
        // let activeDoc = App.activeDoc
        // let obj = activeDoc.inspectingTarget
        // genSetting.changeMemberSetting(activeDoc.packageItem.GetURL(), obj.id, this.setScriptType.selected, obj.name, this.scriptAnnotation.title)
        let sels = App.activeDoc.inspectingTargets
        let obj = sels.get_Item(0);
        let activeDoc = App.activeDoc
        let packageName = activeDoc.packageItem.owner.name;
        let docUrl = activeDoc.docURL;
        let objId = obj.id

        let useable = this.setUseKey.selected
        let change = languageSetting.update(packageName, docUrl, objId, this.languageKey.title, useable)
        if (change) {
            //如果key变化了。
            languageSetting.updateItemTitle(obj, this.languageKey.title)
        }
    }

    private updateUI(): boolean {

        let sels = App.activeDoc.inspectingTargets
        let obj = sels.get_Item(0);
        let activeDoc = App.activeDoc
        let packageName = activeDoc.packageItem.owner.name;
        let docUrl = activeDoc.docURL;
        let objId = obj.id

        let data = languageSetting.get(packageName, docUrl, objId)
        if (data != null) {
            this.setUseKey.selected = data.useable == 1
            this.languageKey.title = data.key
        } else {
            this.setUseKey.selected = false
            this.languageKey.title = ''
        }

        return true; //if everything is ok, return false to hide the inspector
    }
}


class LanguageCustomInspector extends FairyEditor.View.PluginInspector {

    private btnRefresh: FairyGUI.GButton;

    public constructor() {
        super();

        this.panel = FairyGUI.UIPackage.CreateObject("WhootCustomInspector", "LanguageComponentRoot").asCom;
        this.btnRefresh = this.panel.GetChild("btnRefresh").asButton;

        this.btnRefresh.onClick.Add(() => {
            this.updateAll()
        })

        this.updateAction = () => { return this.updateUI(); };
    }

    private updateAll() {
        console.log("")
        let activeDoc = App.activeDoc
        // activeDoc.content

        let packageName = activeDoc.packageItem.owner.name;
        let docUrl = activeDoc.docURL;

        let count = activeDoc.content.numChildren;
        for (let index = 0; index < count; index++) {
            const obj = activeDoc.content.GetChildAt(index)
            if (obj == null) continue
            let data = languageSetting.get(packageName, docUrl, obj.id)
            if (data != null && data.useable == 1) {
                languageSetting.updateItemTitle(obj, data.key)
            }
        }
    }

    private updateUI(): boolean {
        return true; //if everything is ok, return false to hide the inspector
    }
}

class LanguageImageCustomInspector extends FairyEditor.View.PluginInspector {
    private setUseKey: FairyGUI.GButton;
    private ImageKey: FairyEditor.Component.TextInput;

    public constructor() {
        super();

        this.panel = FairyGUI.UIPackage.CreateObject("WhootCustomInspector", "LanguageImageComponent").asCom;
        this.setUseKey = this.panel.GetChild("SetUseKey").asButton;
        this.ImageKey = this.panel.GetChild("ImageKey") as FairyEditor.Component.TextInput

        this.setUseKey.onChanged.Add(() => {
            this.updateData()
        });

        this.ImageKey.onFocusOut.Add(() => {
            this.updateData()
        })


        this.updateAction = () => { return this.updateUI(); };
    }

    private updateData() {
        let sels = App.activeDoc.inspectingTargets
        let obj = sels.get_Item(0);
        let activeDoc = App.activeDoc
        let packageName = activeDoc.packageItem.owner.name;
        let docUrl = activeDoc.docURL;
        let objId = obj.id

        let useable = this.setUseKey.selected
        let change = languageSetting.update(packageName, docUrl, objId, this.ImageKey.title, useable)
        if (change && useable) {
            (obj as FairyEditor.FLoader).clearOnPublish = true;
            App.inspectorView.GetInspector("loader").UpdateUI();
        }
    }

    private updateUI(): boolean {
        let sels = App.activeDoc.inspectingTargets
        let obj = sels.get_Item(0);
        let activeDoc = App.activeDoc
        let packageName = activeDoc.packageItem.owner.name;
        let docUrl = activeDoc.docURL;
        let objId = obj.id

        let data = languageSetting.get(packageName, docUrl, objId)
        if (data != null) {
            this.setUseKey.selected = data.useable == 1
            this.ImageKey.title = data.key
            if (data.useable == 1 && obj instanceof FairyEditor.FLoader) {
                if (!obj.clearOnPublish) {
                    obj.clearOnPublish = true;
                    App.activeDoc.SetModified(true);
                    App.inspectorView.GetInspector("loader").UpdateUI();
                    App.Alert("多语言图片装载器必须勾选发布后清除，避免将图片打进包。");
                }
            }
        } else {
            this.setUseKey.selected = false
            this.ImageKey.title = ''
        }

        return true; //if everything is ok, return false to hide the inspector
    }
}

function runLanguageCustom() {
    //Register a inspector
    App.inspectorView.AddInspector(() => new MembersLanguageCustomInspector(), "MembersLanguageCustomInspectorJS", "多语言设置");
    //Condition to show it
    App.docFactory.ConnectInspector("MembersLanguageCustomInspectorJS", "Button", false, false);
    App.docFactory.ConnectInspector("MembersLanguageCustomInspectorJS", "Label", false, false);
    App.docFactory.ConnectInspector("MembersLanguageCustomInspectorJS", "text", false, false);
    App.docFactory.ConnectInspector("MembersLanguageCustomInspectorJS", "richtext", false, false);
    App.docFactory.ConnectInspector("MembersLanguageCustomInspectorJS", "inputtext", false, false);


    // App.libView.tooltips.
    console.log("多语言设置===")

    // //Register a inspector
    App.inspectorView.AddInspector(() => new LanguageCustomInspector(), "LanguageCustomInspectorJS", "多语言设置");
    //Condition to show it
    App.docFactory.ConnectInspector("LanguageCustomInspectorJS", "component", true, false);

    //Register a inspector
    App.inspectorView.AddInspector(() => new LanguageImageCustomInspector(), "LanguageImageCustomInspectorJS", "多语言图片设置");
    //Condition to show it
    App.docFactory.ConnectInspector("LanguageImageCustomInspectorJS", "loader", false, false);

    // App.workspaceSettings.

    let helpMenu = App.menu.GetSubMenu("help")
    helpMenu.AddItem("Whoot框架帮助", "WhootHelp", () => {
        console.log("点击菜单===")
        UnityEngine.Application.OpenURL("http://172.16.0.149/")
    })
    // let keys = App.preferences.hotkeys.Keys
    // let nmsl = App.preferences.hotkeys.get_Item("languagePath")
    // console.log("nmslnmsl=", nmsl)

    // "none", "graph", "image", "text", "richtext", "inputtext",
    //              "movieclip", "swf", "loader", "group", "list", "loader3D",
    //             "component", "Button", "Label", "Slider", "ProgressBar", "ScrollBar", "ComboBox", "mixed"

}

function languageDestroy() {
    let helpMenu = App.menu.GetSubMenu("help")
    helpMenu.RemoveItem("WhootHelp")
}

export { runLanguageCustom, languageDestroy };