import {invoke} from "@tauri-apps/api/tauri";
import CommanDeffine from "../serportdefine.js";
import EventBus from "../../eventBus.js";
let dev_info={};
async  function Dev_Opt() {
    var Command={command:"start_opt"};
    let data={
        data:Command,
        datatype:"json"
    }
    let message=await invoke("sendtoport_andgetreturn",data);
    if (message.datatype==0x00)
    {
        let ret=JSON.parse(message.content);
        console.log(ret);
    }
    await EnsureNotWorking();
    data.data.command="get_opt";
    message=await invoke("sendtoport_andgetreturn",data);
    var shuttertimes=0;
    if (message.datatype==0x00)
    {
        let ret=JSON.parse(message.content);
        console.log(ret);
        shuttertimes=ret.opt;
    }
    return shuttertimes;


}

async function Set_Gain(gain)
{
    //todo 增益设置
}



async function Get_Data_from_dev(Datatype,shuttertimes,avgnumber=1) {

    let drection=Datatype;
    //获取数据 UPDN
   let  data = {
        data: {
            command: "get_data",
            return_data_type: CommanDeffine.DataTypeforSend.UpDN
        },
        datatype: "json"
    }
    if(drection=="UP")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpDN;
    }else if(drection=="DOWN")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.DOWNDN;
    } else if (drection=="DARK")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpDarkDN;
    } else if (drection=="RAD_UP")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpRadiance;
    } else if (drection=="RAD_DOWN")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.DownRadiance;
    }
    else if (drection=="REF")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.Ref;

    } 

    let message=await invoke("sendtoport_andgetreturn",data);
    while (message.datatype!=0x02)
    {
        message=await invoke("sendtoport_andgetreturn",data);
    }

    return message.data
}



async  function Get_Date_on_Derction(drection,shuttertimes,autoremovedark=false,avgnumber=1) {


    var Command={command:"take_one_job"};
    let data={
        data:Command,
        datatype:"json"
    }
    data.data.command="start_collect";
    if (drection=="UP" || drection == "RAD_UP")
    {
        data.data.direction="up";
        data.data.shutter_time=Number(shuttertimes);

    }else if (drection=="DOWN"||drection == "RAD_DOWN")
    {
        data.data.direction="down";
        data.data.shutter_time=Number(shuttertimes);
    }else if(drection=="DARK")
    {
        data.data.direction="dark";
        data.data.shutter_time=Number(shuttertimes);
    }
    if (autoremovedark&&drection!="DARK")
    {
        data.data.remove_dark="yes";
    }else {
        data.data.remove_dark="no";
    }
    data.data.collect_times=avgnumber;
    let message=await invoke("sendtoport_andgetreturn",data);
    await EnsureNotWorking(Number(shuttertimes)/2);


    //获取数据 UPDN
    data = {
        data: {
            command: "get_data",
            return_data_type: CommanDeffine.DataTypeforSend.UpDN
        },
        datatype: "json"
    }
    if(drection=="UP")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpDN;
    }else if(drection=="DOWN")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.DOWNDN;
    } else if (drection=="DARK")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpDarkDN;
    } else if (drection=="RAD_UP")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpRadiance;
    } else if (drection=="RAD_DOWN")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.DownRadiance;
    } 

    message=await invoke("sendtoport_andgetreturn",data);
    while (message.datatype!=0x02)
    {
        message=await invoke("sendtoport_andgetreturn",data);
    }
    message.data.data.length=dev_info.bandnum;
    return message.data
}

async function Get_Dark_Data(drection,shuttertimes,avgnumber=1) {
    var Command={command:""};
    let data={
        data:Command,
        datatype:"json"
    }
    data.data.command="start_collect_dark";
    if (drection=="UP")
    {
        data.data.direction="up";
        data.data.shutter_time=Number(shuttertimes);

    }else if (drection=="DOWN")
    {
        data.data.direction="down";
        data.data.shutter_time=Number(shuttertimes);
    }else if(drection=="DARK")
    {
        data.data.direction="dark";
        data.data.shutter_time=Number(shuttertimes);
    }

    data.data.collect_times=Number(avgnumber);
    let message=await invoke("sendtoport_andgetreturn",data);
    await EnsureNotWorking(Number(shuttertimes)/2);
    
    //获取数据 UPDN
    data = {
        data: {
            command: "get_data_dark",
            return_data_type: CommanDeffine.DataTypeforSend.UpDN
        },
        datatype: "json"
    }
    if(drection=="UP")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpDN;
    }else if(drection=="DOWN")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.DOWNDN;
    } else if (drection=="DARK")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpDarkDN;
    }

    message=await invoke("sendtoport_andgetreturn",data);
    while (message.datatype!=0x02)
    {
        message=await invoke("sendtoport_andgetreturn",data);
    }
    EventBus.emit('showbox', {title: "系统", body: "采集Dark 完成",interval:10});
    EventBus.emit('setprogressbar',{percent:100,info:""});
    EventBus.emit('SetMenubutton', {name:"DC",state:"OK"});
    return message.data
}


async function Get_Flat_Data(drection,shuttertimes,avgnumber=1) {
    var Command={command:""};
    let data={
        data:Command,
        datatype:"json"
    }
    data.data.command="start_collect_flat";
    if (drection=="UP")
    {
        data.data.direction="up";
        data.data.shutter_time=Number(shuttertimes);

    }else if (drection=="DOWN")
    {
        data.data.direction="down";
        data.data.shutter_time=Number(shuttertimes);
    }else if(drection=="DARK")
    {
        data.data.direction="dark";
        data.data.shutter_time=Number(shuttertimes);
    }

    data.data.collect_times=Number(avgnumber);
    let message=await invoke("sendtoport_andgetreturn",data);
    await EnsureNotWorking(Number(shuttertimes)/2);


    

    //获取数据 UPDN
    data = {
        data: {
            command: "get_data_flat",
            return_data_type: CommanDeffine.DataTypeforSend.UpDN
        },
        datatype: "json"
    }
    if(drection=="UP")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpDN;
    }else if(drection=="DOWN")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.DOWNDN;
    } else if (drection=="DARK")
    {
        data.data.return_data_type=CommanDeffine.DataTypeforSend.UpDarkDN;
    }

    message=await invoke("sendtoport_andgetreturn",data);
    while (message.datatype!=0x02)
    {
        message=await invoke("sendtoport_andgetreturn",data);
    }
   



    EventBus.emit('showbox', {title: "系统", body: "采集FLat 完成",interval:10});
    EventBus.emit('setprogressbar',{percent:100,info:""});
    EventBus.emit('SetMenubutton', {name:"WR",state:"OK"});
    return message.data
}

async function Get_Device_Info() {
    var Command={command:"get_sensor_info"};
    let data={
        data:Command,
        datatype:"json"
    }
    let message=await invoke("sendtoport_andgetreturn",data);
    if (message.datatype==108)
    {
        return {error:"error"};
    }
    let json=JSON.parse(message.content);
    dev_info=json;
    return json;
}












async function EnsureNotWorking(waittime=1000){
    let data={
        data:{
            command:"get_caiji_state"
        },
        datatype:"json"
    }
    let message=await invoke("sendtoport_andgetreturn",data);
    let ret=JSON.parse(message.content);

    while (ret.caiji_state!="finish")
    {

        //发送进度信息
        let dataforprocess={
            percent:ret.percent,
            info:ret.info
        }
        EventBus.emit('setprogressbar',dataforprocess);
        await delay(waittime);
        message=await invoke("sendtoport_andgetreturn",data);

        if (message.content=="g time no data retrun")
        {
            alert("长时间没有数据返回");
            return;
        }
        ret=JSON.parse(message.content);

    }
    let dataforprocess={
        percent:100,
        info:""
    }
    EventBus.emit('setprogressbar',dataforprocess);
}
function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function Set_Dev_Type(Devtype)
{
   let message=await invoke("setdevtype",{devtype:Devtype});
    return message;

}






async function Set_Weave_Coeff(specnumber,a0,a1,a2,a3){
    var Command={};
    Command.specnumber=Number(specnumber);
    Command.a0=Number(a0);
    Command.a1=Number(a1);
    Command.a2=Number(a2);
    Command.a3=Number(a3);

    
    

    await invoke("set_weave_coeff",Command);
}





export default {
    Dev_Opt,
    Get_Date_on_Derction,
    Get_Device_Info,
    Get_Dark_Data,
    Get_Flat_Data,
    Get_Data_from_dev,
    Set_Dev_Type,
    Set_Gain,
    Set_Weave_Coeff



}
