"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const FairyGUI = CS.FairyGUI;
const FairyEditor = CS.FairyEditor;
const FileUtils_1 = require("./FileUtils");
const UIWindow_1 = require("./UIWindow");
const App = FairyEditor.App;
class FindFailureAssetsWnd extends UIWindow_1.default {
    constructor() {
        super(new UIWindow_1.WindData("Extend", "FindFailureAssets"));
    }
    static show() {
        let url = FileUtils_1.default.joinUrl("Extend", "FindFailureAssets");
        if (UIWindow_1.default.FindUrl(url) == undefined) {
            UIWindow_1.default.add(new this);
        }
        super.show(url);
    }
    state;
    check;
    progress;
    resList;
    assets = new Map();
    onInit() {
        super.onInit();
        this.state = this.contentPane.GetController("state");
        this.check = this.contentPane.GetChild("check").asButton;
        this.progress = this.contentPane.GetChild("progress").asProgress;
        this.resList = this.contentPane.GetChild("resList").asList;
        this.resList.itemRenderer = this.onItemRenderer.bind(this);
        this.resList.onClickItem.Add(this.onListClickItem.bind(this));
        this.check.onClick.Add(this.onCheckClick.bind(this));
        let cancel = this.contentPane.GetChild("close").asButton;
        cancel.onClick.Add(this.onCancelClick.bind(this));
        this.resList.SetVirtual();
    }
    onShown() {
        super.onShown();
    }
    onHide() {
        super.onHide();
        this.resList.data = null;
        this.resList.numItems = 0;
    }
    async onCheckClick() {
        if (this.state.selectedIndex == 0) {
            this.state.selectedIndex = 1;
            this.assets.clear();
            let pkgs = App.project.allPackages;
            this.progress.value = 0;
            this.progress.max = FileUtils_1.default.getAllPackageItemsCount();
            for (let index = 0; index < pkgs.Count; index++) {
                let pkg = pkgs.get_Item(index);
                if (this.state.selectedIndex == 0)
                    break;
                for (let j = 0; j < pkg.items.Count; j++) {
                    if (this.state.selectedIndex == 0)
                        break;
                    let item = pkg.items.get_Item(j);
                    this.progress.value += 1;
                    if (item.type != CS.FairyEditor.FObjectType.COMPONENT) {
                        continue;
                    }
                    //@ts-ignore
                    this.getFailureAssets(item, this.assets, 0);
                    if (this.progress.value % 30 == 0) {
                        await FileUtils_1.default.sleep(0);
                    }
                }
            }
            App.consoleView.Log("查找完毕");
            this.state.selectedIndex = 0;
            let asset = Array.from(this.assets).map(item => item[1]);
            this.resList.data = asset;
            this.resList.numItems = asset.length;
            this.contentPane.GetChild("projectCount").asTextField.templateVars = FileUtils_1.default.getTemplateVars(['value'], [asset.length.toString()]);
        }
        else {
            this.state.selectedIndex = 0;
        }
    }
    onItemRenderer(index, obj) {
        let data = this.resList.data[index];
        obj.data = data;
        let packageItem = App.project.GetItemByURL(data.url);
        obj.GetChild("title").text = packageItem.name;
        obj.__onDispose = () => {
            obj.GetChild("title").text = '';
            obj.data = null;
        };
    }
    onListClickItem(item) {
        let data = item.data.data;
        let packageItem = App.project.GetItemByURL(data.url);
        App.consoleView.Log(FileUtils_1.default.getUBBUrl(packageItem.GetURL(), `包含无效URL的组件:${packageItem.name}`));
        for (let index = 0; index < data.failure.length; index++) {
            App.consoleView.Log('无效URL:' + data.failure[index]);
        }
    }
    onCancelClick() {
        let self = this;
        this.Hide();
        FairyGUI.Timers.inst.Add(0.05, 1, () => {
            UIWindow_1.default.remove(self);
        });
    }
    /**查找失效资源 */
    getFailureAssets(item, map, quertIndex) {
        let xml = CS.FairyEditor.XMLExtension.Load(item.file);
        if (!xml) {
            App.consoleView.LogError(`PackageItem找不到配置文件. Name:${item.name} Index:${quertIndex} File:${item.file}`);
            return quertIndex;
        }
        let rootElements = xml.Elements();
        for (let index = 0; index < rootElements.Count; index++) {
            let child = rootElements.get_Item(index);
            if (child.name != 'displayList')
                continue;
            let childElements = child.Elements();
            for (let index = 0; index < childElements.Count; index++) {
                const element = childElements.get_Item(index);
                if (element.name == 'loader' || element.name == 'loader3D') {
                    if (element.GetAttribute('clearOnPublish'))
                        continue;
                    let url = element.GetAttribute('url');
                    if (!url)
                        continue;
                    //没清理依赖
                    let packageItem = App.project.GetItemByURL(url);
                    if (!packageItem) {
                        if (!map.has(item.GetURL())) {
                            map.set(item.GetURL(), {
                                url: item.GetURL(),
                                failure: []
                            });
                        }
                        map.get(item.GetURL()).failure.push(`name:${element.GetAttribute("name")} file:${element.GetAttribute("fileName")} url:${url}`);
                    }
                    else if (!map.has(packageItem.GetURL())) {
                        if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
                            quertIndex = this.getFailureAssets(packageItem, map, quertIndex);
                        }
                        quertIndex += 1;
                    }
                }
                else {
                    let src = element.GetAttribute("src");
                    if (!src) {
                        //系统资源 不需要依赖
                        continue;
                    }
                    let pkg = element.GetAttribute("pkg");
                    if (!pkg) {
                        //自己依赖包的资源 所以不需要pkg
                        pkg = item.owner.id;
                    }
                    let url = `ui://${pkg}${src}`;
                    if (src == null)
                        App.consoleView.Log(element.name);
                    let packageItem = App.project.GetItemByURL(url);
                    if (!packageItem) {
                        if (!map.has(item.GetURL())) {
                            map.set(item.GetURL(), {
                                url: item.GetURL(),
                                failure: []
                            });
                        }
                        map.get(item.GetURL()).failure.push(`name:${element.GetAttribute("name")} file:${element.GetAttribute("fileName")} url:${url}`);
                    }
                    else if (!map.has(packageItem.GetURL())) {
                        if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
                            quertIndex = this.getFailureAssets(packageItem, map, quertIndex);
                        }
                        quertIndex += 1;
                    }
                }
            }
        }
        return quertIndex;
    }
}
exports.default = FindFailureAssetsWnd;
