import {config} from './main';
import FairyEditor = CS.FairyEditor;
import FindFailureAssetsWnd from "./FindFailureAssetsWnd";
import UIWindow from "./UIWindow";
import FindDependencyQueryWnd from "./FindDependencyQueryWnd";
import PsdToFgui from './PsdToFgui';

const App = FairyEditor.App;
const XGameMenuName: string = "XGameMenu";
const NoExportKey: string = "noexport"; // 不导出的贴图

export default class MenuExtension {
    public start(): void {
        this.registerMenu();
    }

    private registerMenu() {
        // 主菜单不能加回调事件，否则会报错
        App.menu.AddItem("XGame", XGameMenuName, 5, true, null);

        let xgMenu = App.menu.GetSubMenu(XGameMenuName);
        xgMenu.AddItem("psd->fgui", "psd2fgui", this.onPsd2Fgui.bind(this));
        xgMenu.AddSeperator();
        xgMenu.AddItem("贴图检测", "CollectAtlas", this.onCollectAtlas.bind(this));
        // xgMenu.AddItem("测试导入", "TestImport", () => {
        //     // let cPackage = FairyEditor.App.project.AddPackage("F:/TJWork/psd2fgui/test/test-fairypackage");
        //     let cPackage = FairyEditor.App.project.allPackages[0];
        //     let importQueue = FairyEditor.ResourceImportQueue.Create(cPackage);
        //     importQueue.Add("F:/TJWork/psd2fgui/test/test.fairypackage");
        //     importQueue.Process((items)=>{
        //         App.consoleView.Log(`11111111`);
        //     });
        //     App.consoleView.Log(`click TestImport pkgItem:${cPackage?.name ?? "0000"}`);
        //     // FairyEditor.App.libView.ShowImportResourcesDialog(pkgItem, "F:/CCWork/zhetian_client/fgui-project/assets/test-fairypackage");
        //     // FairyEditor.BuilderUtil.OpenURL("file://F:/TJWork/psd2fgui/test/test.fairypackage");
        // });
        xgMenu.AddSeperator();
        xgMenu.AddItem("查找组件依赖关系", "findDependency", this.onFindDependency.bind(this));
        xgMenu.AddItem("查找无效组件", "findFailureAssets", this.onFindFailureAssets.bind(this));
    }
    private onPsd2Fgui() {
        let psd2fgui = new PsdToFgui();
        psd2fgui.start();
    }

    private onFindDependency() {
        FindDependencyQueryWnd.show()
    }
    private onFindFailureAssets() {
        FindFailureAssetsWnd.show()
    }
    private onCollectAtlas() {
        let pkgs = App.project.allPackages;
        for (let index = 0; index < pkgs.Count; index++) {
            let pkg = pkgs.get_Item(index);
            for (let j = 0; j < pkg.items.Count; j++) {
                let item = pkg.items.get_Item(j);
                if (item.type != CS.FairyEditor.FObjectType.IMAGE && item.type != CS.FairyEditor.FObjectType.COMPONENT) {
                    continue;
                }
                if (item.path.toLowerCase().includes(NoExportKey)) {
                    if (!pkg.publishSettings.excludedList.Contains(item.id)) {
                        pkg.publishSettings.excludedList.Add(item.id);
                        App.consoleView.Log(`排除资源. pkg:${pkg.name} name:${item.name} file:${item.fileName}`);
                    }
                    continue;
                }
                if (item.type != CS.FairyEditor.FObjectType.IMAGE) {
                    continue
                }
                let isPng = item.fileName.toLowerCase().endsWith(".png");
                if (item.GetAtlasIndex() == 0 && isPng) {
                    continue;
                }
                let imgAsset = item.GetAsset() as CS.FairyEditor.ImageAsset;
                imgAsset.atlas = isPng ? "0" : "9";
                item.SetChanged();
                App.consoleView.Log(`Image pkg:${pkg.name} fileName:${item.fileName} atlas:${item.folderAtlas} support:${item.supportAtlas} Index:${item.GetAtlasIndex()} file:${imgAsset.file} atlas:${imgAsset.atlas}`);
            }
            pkg.SetChanged();
            pkg.Save();
        }
        App.RefreshProject();
    }

    onDestroy() {
        UIWindow.delAll([]);
        App.menu.RemoveItem(XGameMenuName);
    }
}
