import { queenApi } from "queenjs";
import { BusController } from "./natsController";

export class  TreeController {
    _bus: BusController
    constructor(bus: BusController) {
        this._bus = bus
    }

    async selectOneMat() {
        const bus = this._bus;
        const route = await bus.requestApi("queentree.local.router", {name:"selmatblack"})
        if (!route || !route.Url ) return;
        const url = route.Url;
        const payload = {}
        const ret:any = await bus.RequestWebView("selmatblack", {height: 600, width: 900, payload, url, alwaysOnTop:true,})
        console.log("selmat=>", ret);

        if (ret.payload) {
            ret.payload.uvMap = "uv";
            ret.payload.classType = "pbr";
            ret.payload.type = "meta";
        }
        
        return ret.payload;
    }

    async selectOnePack() {
        const bus = this._bus;
        const route = await bus.requestApi("queentree.local.router", {name:"selpacksceneblack"})
        if (!route || !route.Url ) return;
        let url:string = route.Url;
        url = url.replace("#/selpackscene", "#/selectPack")
        const payload = {}
        const ret:any = await bus.RequestWebView("selpacksceneblack", {dev:true, height: 600, width: 900, payload, url, alwaysOnTop:true,})
        console.log("selpackprodblack=>", ret);
        return ret.payload;
    }

    async selectOnePackScene() {
        const bus = this._bus;
        const route = await bus.requestApi("queentree.local.router", {name:"selpacksceneblack"})
        if (!route || !route.Url ) return;
        const url = route.Url;
        const payload = {}
        const ret:any = await bus.RequestWebView("selpacksceneblack", {dev:true, height: 600, width: 900, payload, url, alwaysOnTop:true,})
        console.log("selpackprodblack=>", ret);
        return ret.payload;
    }

    async  SelectedPackComp(pack:any, prodId:string, compId:string) {
        queenApi.showLoading("数据发送中")
        const bus = this._bus;

        const sid = bus.getQuery("sid")
    
        const ok = await bus.Public("packcomp.update." + sid, {pack, prodId, compId})
        queenApi.hideLoading();
    
        if (!ok) {
            queenApi.messageError("数据接收方已关闭");
            return;
        }
        queenApi.messageSuccess("数据发送成功");
    }
    
     async CloseWindow() {
        const bus = this._bus;
        const sid = bus.getQuery("sid")
        const ok = await bus.Public("tree.selector.close", {sid})
        queenApi.hideLoading();
        if (!ok) {
            queenApi.messageError("窗口关闭失败,请点击右上角关闭按钮");
            return;
        }
        return true;
    }
    
    async  CloseTreeOpen(name:string, payload?:any) {
        const bus = this._bus;
        await bus.Public("webview.close." + name, {name: name, payload})
    }
    
    async  subscribeData(callback:any){
        const bus = this._bus;
        return await  bus.subscribe('tree.savaas.update.mat',callback)
    }
    
     async subscribeTreeOpenFor(name:string, callback:any){
        const bus = this._bus;
        return await  bus.subscribe('webview.opened.'+name,callback)
    }
    
     async GetSaveMatData() {
        const bus = this._bus;
        const sid = bus.getQuery("sid")
        return await this.GetSessionData(sid);
    }
    
     async GetPackListQueryData() {
        const bus = this._bus;
        const sid = bus.getQuery("sid")
        return await this.GetSessionData(sid);
    }
    
     async GetSessionData(sid:string) {
        const bus = this._bus;

        const matgroup = await bus.requestApi("tree.session.data", {sid})
        queenApi.hideLoading();
        if (!matgroup) {
            queenApi.messageError("获取数据失败");
            return;
        }
        return matgroup;
    }
    
    async  SelectedPackList(list :any) {
        const bus = this._bus;

        queenApi.showLoading("数据发送中")
    
        const sid = bus.getQuery("sid")
    
        const ok = await bus.Public("packlist.update." + sid, list)
        queenApi.hideLoading();
    
        if (!ok) {
            queenApi.messageError("数据接收方已关闭");
        } else {
            queenApi.messageSuccess("数据发送成功");
        }
        setTimeout(()=>{
            this.CloseWindow();
        }, 1000)
    }
}