use super::mydefine::*;
pub mod is11base;
pub mod is11server;
use super::mylog::*;
use is11server::get_shuttertime;
use serde_json::json;
use std::ffi::CStr;

pub fn sendtoport_andgetreturn(
    data: serde_json::Value,
    datatype: serde_json::Value,
) -> Result<RetStruct, String> {
    let command = data.get("command").unwrap().to_string();
    println!("start_collect data:{}", data.to_string());
    match command.as_str() {

        "\"start_collect_flat\"" => {
            return start_collect_flat(data, datatype);
        },  

        "\"start_collect_dark\"" => {
            return start_collect_dark(data, datatype);
        },
        "\"get_sensor_info\"" => {
            is11base::is11_init();
            logtorust("init is11 ok".to_string());
            let sensor_info = is11base::is11_get_sensor_info();
            let sensor_name_cstr = unsafe { CStr::from_ptr(sensor_info.SensorName.as_ptr()) };
            let sensor_name = sensor_name_cstr.to_str().unwrap();
            let serilnumber_cstr = unsafe { CStr::from_ptr(sensor_info.serialnumber.as_ptr()) };
            let serilnumber = serilnumber_cstr.to_str().unwrap();
            //定义一个json对象
            let jsonforret = json!({
                "bochangxishu":{
                    "a0":sensor_info.a1,
                    "a1":sensor_info.a2,
                    "a2":sensor_info.a3,
                    "a3":sensor_info.a4,
                },
                "serialnumber":serilnumber,
                
                "name":"IS11",
                // "name":sensor_name,
                "version":"1.0.0",
                "return_data_type":0,
                "work_mode":"advanced_mode",
                "sensor":"JZ-IS11",

            });
            logtorust(format!("get_sensor_info:{}", jsonforret.to_string()));
            return Ok(RetStruct {
                datatype: 0,
                content: jsonforret.to_string(),
                data: IS11DataStruct::default(),
            });
        }
        "\"start_opt\"" => {
            is11server::opt_sensor(90);
            let jsonforret = json!({
                "command":"start_opt"

            });
            return Ok(RetStruct {
                datatype: 0,
                content: jsonforret.to_string(),
                data: IS11DataStruct::default(),
            });
        }
        "\"get_opt\"" => {
            println!("get_opt");
            let ret = get_opt_result();
            return ret;
        }
        "\"start_collect\"" => {
            return start_collect(data, datatype);
        }

        "\"get_caiji_state\"" => {
            let (caiji_state, caiji_state_str,percent) = is11server::get_now_stat();
            let jsonforret = json!({
                "caiji_state":caiji_state,
                "info":caiji_state_str,
                "percent":percent,
                "return_data_type":0,
            });
            logtorust(format!("get_caiji_state:{}", jsonforret.to_string()));
            return Ok(RetStruct {
                datatype: 0,
                content: jsonforret.to_string(),
                data: IS11DataStruct::default(),
            });
        }
        "\"get_data\"" => {
            return get_data(data, datatype);
        },
        "\"get_data_flat\"" => {
            return get_data_flat(data, datatype);
        },
        "\"get_data_dark\"" => {
            return get_data_dark(data, datatype);
        },
        "\"set_shutter_time\"" => {
            return set_shutter_time(data, datatype);
        },
        _ => {
            return Err("command not found ".to_string() + "command is:" + &command);
        }
    }


}

pub fn get_opt_result() -> Result<RetStruct, String> {
    let shuttertime = is11server::get_shuttertime();

    let jsonret = json!({
        "opt":shuttertime,

    });



    return Ok(RetStruct {
        datatype: 0,
        content: jsonret.to_string(),
        data: IS11DataStruct::default(),
    });
}
pub fn set_shutter_time(data: serde_json::Value,
    datatype: serde_json::Value) -> Result<RetStruct, String> {

    
        let shuttertime: u32;
        if data.get("shutter_time").is_none() {
            shuttertime = get_shuttertime() as u32;
        } else {
            shuttertime = data.get("shutter_time").unwrap().as_u64().unwrap() as u32;
            is11server::set_shutter_time(shuttertime);
        }


    let jsonret = json!({
        "shuttertime":"ok",

    });



    return Ok(RetStruct {
        datatype: 0,
        content: jsonret.to_string(),
        data: IS11DataStruct::default(),
    });
}
pub fn start_collect(
    data: serde_json::Value,
    datatype: serde_json::Value,
) -> Result<RetStruct, String> {


    let shuttertime: u32;
    if data.get("shutter_time").is_none() {
        shuttertime = get_shuttertime() as u32;
    } else {
        shuttertime = data.get("shutter_time").unwrap().as_u64().unwrap() as u32;
    }
    //判断是否有这个参数

    let removedark: bool;
    if data.get("remove_dark").is_none() {
        removedark = false;
    } else {
        if data.get("remove_dark").unwrap().is_boolean() {
            removedark = data.get("remove_dark").unwrap().as_bool().unwrap();
        } else if data.get("remove_dark").unwrap().is_string() {
            let remove_dark_str = data.get("remove_dark").unwrap().as_str().unwrap();
            if remove_dark_str == "yes" {
                removedark = true;
            } else {
                removedark = false;
            }
        } else {
            removedark = false;
        }
    }

    let computeflat = is11server::get_is_computeref();
    logtorust(format!("start_collect,shutter time is:{}", shuttertime));
    if !data.get("collect_times").is_none() {
        if data.get("collect_times").unwrap().is_u64() {
            let collect_times = data.get("collect_times").unwrap().as_u64().unwrap() as u32;
            is11server::set_average_number(collect_times,collect_times,collect_times );
        } 
         

        
    }


    is11server::collect(shuttertime, removedark, computeflat);
    let jsonret = json!({
        "command":"start_collect"

    });
    return Ok(RetStruct {
        datatype: 0,
        content: jsonret.to_string(),
        data: IS11DataStruct::default(),
    });
}

pub fn start_collect_dark(
    data: serde_json::Value,
    datatype: serde_json::Value,
) -> Result<RetStruct, String> {
    let shuttertime: u32;
    if data.get("shutter_time").is_none() {
        shuttertime = get_shuttertime() as u32;
    } else {
        shuttertime = data.get("shutter_time").unwrap().as_u64().unwrap() as u32;
    }
    //判断是否有这个参数
    if !data.get("collect_times").is_none() {
        if data.get("collect_times").unwrap().is_u64() {
            let collect_times = data.get("collect_times").unwrap().as_u64().unwrap() as u32;
            is11server::set_average_number(collect_times,collect_times,collect_times );
        }      
    }

    is11server::collcect_dark(shuttertime);
    let jsonret = json!({
        "command":"start_collect_dark"

    });
    return Ok(RetStruct {
        datatype: 0,
        content: jsonret.to_string(),
        data: IS11DataStruct::default(),
    });

}


pub fn start_collect_flat(
    data: serde_json::Value,
    datatype: serde_json::Value,
) -> Result<RetStruct, String> {
    let shuttertime: u32;
    if data.get("shutter_time").is_none() {
        shuttertime = get_shuttertime() as u32;
    } else {
        shuttertime = data.get("shutter_time").unwrap().as_u64().unwrap() as u32;
    }
    //判断是否有这个参数
    if !data.get("collect_times").is_none() {
        if data.get("collect_times").unwrap().is_u64() {
            let collect_times = data.get("collect_times").unwrap().as_u64().unwrap() as u32;
            is11server::set_average_number(collect_times,collect_times,collect_times );
        }      
    }

    is11server::collcect_flat(shuttertime);
    let jsonret = json!({
        "command":"start_collect_flat"

    });
    return Ok(RetStruct {
        datatype: 0,
        content: jsonret.to_string(),
        data: IS11DataStruct::default(),
    });

}





pub fn get_data(data: serde_json::Value, datatype: serde_json::Value) -> Result<RetStruct, String> {
    let is11_data = is11server::get_data();

    let jsonret = json!({
        "command":"get_data"

    });

    return Ok(RetStruct {
        datatype: 2,
        content: jsonret.to_string(),
        data: is11_data,
    });
}

pub fn get_data_flat(data: serde_json::Value, datatype: serde_json::Value) -> Result<RetStruct, String> {
    let is11_data = is11server::get_data_flat();

    let jsonret = json!({
        "command":"get_data_flat"

    });

    return Ok(RetStruct {
        datatype: 2,
        content: jsonret.to_string(),
        data: is11_data,
    });
}

pub fn get_data_dark(data: serde_json::Value, datatype: serde_json::Value) -> Result<RetStruct, String> {
    let is11_data = is11server::get_data_dark();

    let jsonret = json!({
        "command":"get_data_dark"

    });

    return Ok(RetStruct {
        datatype: 2,
        content: jsonret.to_string(),
        data: is11_data,
    });
}