import FairyGUI = CS.FairyGUI;
import FairyEditor = CS.FairyEditor;
import System = CS.System;
import genSetting from './GenCodeSettings'

const App = FairyEditor.App;
App.pluginManager.LoadUIPackage(App.pluginManager.basePath + "/" + eval("__dirname") + '/WhootCustomInspector')

class GenCodeCustomInspector extends FairyEditor.View.PluginInspector {
    private combo: FairyGUI.GComboBox;
    private setScriptName: FairyGUI.GButton
    private scriptName: FairyEditor.Component.TextInput;
    private scriptAnnotation: FairyEditor.Component.TextInput;

    public constructor() {
        super();

        this.panel = FairyGUI.UIPackage.CreateObject("WhootCustomInspector", "Component1").asCom;
        this.combo = this.panel.GetChild("genType").asComboBox;
        this.setScriptName = this.panel.GetChild("SetScriptName").asButton
        this.scriptName = this.panel.GetChild("scriptName") as FairyEditor.Component.TextInput
        this.scriptAnnotation = this.panel.GetChild("scriptAnnotation") as FairyEditor.Component.TextInput
        // this.combo = this.panel.GetChild("SetScriptType").asComboBox;
        let self = this
        this.combo.onChanged.Add(() => {
            self.updateData()
        });
        this.setScriptName.onChanged.Add(() => {
            this.updateData()
        });

        this.scriptName.onFocusOut.Add(() => {
            this.updateData()
        })
        
        this.scriptAnnotation.onFocusOut.Add(() => {
            this.updateData()
        })

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

    private updateData() {
        let activeDoc = App.activeDoc
        let obj = activeDoc.inspectingTarget as FairyEditor.FComponent
        //console.log("update==2", this.scriptName.title)
        genSetting.changeComponentSetting(obj.resourceURL, this.combo.value, activeDoc.packageItem.name, this.setScriptName.selected, this.scriptName.title, this.scriptAnnotation.title)
    }

    private updateUI(): boolean {
        let sels = App.activeDoc.inspectingTargets
        let obj = sels.get_Item(0);
        var data = genSetting.getComponentSetting(obj.resourceURL)
        if (data != null) {
            this.combo.value = data.scriptType
            this.setScriptName.selected = data.isCustomName
            this.scriptName.title = data.customName
            this.scriptAnnotation.title = data.annotation
        } else {
            this.combo.value = null;
            this.setScriptName.selected = false
            this.scriptName.title = ""
            this.scriptAnnotation.title = ""
        }
        return true; //if everything is ok, return false to hide the inspector
    }
}

// class MembersGenCodeCustomInspector extends FairyEditor.View.PluginInspector {
//     private setScriptType: FairyGUI.GButton;
//     private scriptAnnotation: FairyEditor.Component.TextInput;

//     public constructor() {
//         super();

//         this.panel = FairyGUI.UIPackage.CreateObject("WhootCustomInspector", "Component2").asCom;
//         this.setScriptType = this.panel.GetChild("SetScriptType").asButton;
//         this.scriptAnnotation = this.panel.GetChild("scriptAnnotation") as FairyEditor.Component.TextInput

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

//         this.scriptAnnotation.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)
//     }

//     private updateUI(): boolean {
//         // let sels = App.activeDoc.inspectingTargets
//         // let obj = sels.get_Item(0);
//         let activeDoc = App.activeDoc
//         let obj = activeDoc.inspectingTarget
//         let data = genSetting.getMemberSetting(activeDoc.packageItem.GetURL(), obj.id)
//         if (data != null) {
//             this.setScriptType.selected = data.useCustomScript
//             this.scriptAnnotation.title = data.annotation
//             genSetting.changeMemberSetting(activeDoc.packageItem.GetURL(), obj.id, this.setScriptType.selected, obj.name, this.scriptAnnotation.title)
//         } else {
//             this.setScriptType.selected = false
//             this.scriptAnnotation.title = ""
//         }

//         //this.setScriptType.selected = obj.customData == "1"

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


function runGenCodeCustom() {
    // //Register a inspector
    // App.inspectorView.AddInspector(() => new MembersGenCodeCustomInspector(), "MembersGenCodeCustomInspectorJS", "成员脚本生成设置");
    // //Condition to show it
    // App.docFactory.ConnectInspector("MembersGenCodeCustomInspectorJS", "component", false, false);

    //Register a inspector
    App.inspectorView.AddInspector(() => new GenCodeCustomInspector(), "GenCodeCustomInspectorJS", "组件脚本生成设置");
    //Condition to show it
    App.docFactory.ConnectInspector("GenCodeCustomInspectorJS", "component", true, false);

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

    //FairyEditor.FPackageItem(context.data)
}


export { runGenCodeCustom };