
import {invoke} from "@tauri-apps/api/tauri";
import {ref} from "vue";

import {appWindow, LogicalSize, WebviewWindow} from "@tauri-apps/api/window";
import setWavelenth from "./components/menubox/SetWavelenth.vue";
import SetName  from "./components/menubox/SetName.vue";
import SetWorkmode  from "./components/menubox/SetWorkmode.vue";
import setCalibrate from "./components/menubox/SetCalibrate.vue";
import setCalibrateHH3 from "./components/menubox/SetCalibrateHH3.vue";
import setWavelenthCoeff from "./components/menubox/SetWavelenthcoeff.vue";
import Help from "./components/menubox/help.vue";
import Greet from "./components/Greet.vue";
import EventBus from "./eventBus.js";
import {$ref} from "vue3-json-editor/dist/vue3-json-editor.cjs.js";
//引入

export default {
    async onmenuclicked(command){

        console.log("menuclicked main "+command.name)
        if (command.type=="Set" )
        {
            if (!this.isDevOpen())
            {
                return;
            }
        }
        if (command.type=="File"){
            if (command.name=="Advance"){
                this.modaltitle="高级测试模式";
                this.modalcomponent=Greet;
                this.showModal();
            }
        }

        if (command.type=="info"){
            if (command.name=="help"){
                this.modaltitle="关于";
                this.modalcomponent=Help;
                this.showModal();
            }
        }




        // if (command.name=="setting"){
        //
        //     this.modaltitle="设置波长";
        //     this.modalcomponent=setWavelenth
        //     this.showModal();
        // }
        if (command.name=="DevInfo"){

            let iscommenopen=this.$refs.SiderComponend.SerialInfo.isopen;
            if (!iscommenopen)
            {
                alert("请先打开设备");
                return
            }
            this.modaltitle="设置设备信息";
            this.modalcomponent=SetName
            await this.$nextTick();

            this.$refs.Commancompent.name=this.$refs.SiderComponend.Devinfo.name;
            this.$refs.Commancompent.version=this.$refs.SiderComponend.Devinfo.Version;
            this.$refs.Commancompent.upother=this.$refs.SiderComponend.GetSenSorInfo

            this.showModal();

        }
        if (command.name=="Workmode"){
            this.modaltitle="设置工作模式";

            this.modalcomponent=SetWorkmode
            await this.$nextTick();
            this.$refs.Commancompent.GetSenSorInfo();
            this.showModal();
        }
        

        
        if(command.name=="Weavelenth")
        {
            if  (!this.isinAdvanceMode()) return;
            this.modaltitle="设置波长";
            this.modalcomponent=setWavelenth
            await this.$nextTick();
          //  this.$refs.Commancompent.GetSenSorInfo();
            this.showModal();
        }
        if(command.name=="Weavelenthcoeff")
            {
                if  (!this.isinAdvanceMode()) return;
                this.modaltitle="设置波长";
                this.modalcomponent=setWavelenthCoeff
                await this.$nextTick();
              //  this.$refs.Commancompent.GetSenSorInfo();
                this.showModal();
            }
        
        if (command.name=="Calibrate")
        {
            if  (!this.isinAdvanceMode()) return;
            this.modaltitle="能量定标";
            this.modalcomponent=setCalibrate
            await this.$nextTick();
            // this.$refs.Commancompent.GetSenSorInfo();
            this.showModal();


        }
        if (command.name=="CalibrateHH3")
        {
            if  (!this.isinAdvanceMode()) return;
            this.modaltitle="能量定标";
            this.modalcomponent=setCalibrateHH3
            await this.$nextTick();
            // this.$refs.Commancompent.GetSenSorInfo();
            this.showModal();


        }

        if (command.type=="Work")
        {
            if (!this.isDevOpen())
                {
                    return;
                }
            EventBus.emit('LetSiderDo',command.name);
        }

        







    },

    async newwindow() {
        const webview = new WebviewWindow("element", {
            url: "/element",
            // 可自行添加属性配置     窗口配置
            width: 2000,
            height: 900,
            //center:true,
            //decorations:false
        });

        await webview.once("tauri://created", function () {

            // webview window successfully created
            // 窗口创建成功 打印1
            console.log(1);
        });
        await webview.once("tauri://error", function (e) {
            // an error happened creating the webview window
            // 窗口创建失败 打印2
            console.log(2);
        });


        // testWindow.open();
    },
    showModal() {
        this.modal=!this.modal;

      //  alert("showModal")
    },
    isDevOpen(){
        let iscommenopen=this.$refs.SiderComponend.SerialInfo.isopen;
        if (!iscommenopen)
        {
            alert("请先打开设备");
            return  false;
        }
        return true;
    },
    isinAdvanceMode(){
        let isadvance=this.$refs.SiderComponend.Devinfo.work_mode;
        if(isadvance!="advanced_mode")
        {
            alert("请先切换到高级模式");
            return false;
        }
        return true;

    },
    inittour(){
        var steps=[
            {
                title: 'Save',
                description: 'Save your changes.',
                target:()=>this.$refs.SiderComponend.value&& this.$refs.SiderComponend.value.$el,
            }
        ]
        this.tourdata.steps=steps;


    },
    siderresize(){
        this.$refs.HyperSpectralMainView.echartresize();
    },
    async testinmpt(){
        let x=[0.0,0.5, 0.569, 1.138, 1.707, 2.276, 2.845];
        let y=[0.0, 0.4,0.5, 1.0, 0.5, 0.0, -0.5];
        let cc=await invoke("interpolate_spline",{x:x,y,step:0.1});
        console.log(cc);

    }


}
