import { GET,POST  } from "@/utils/http";
import client from "@/stores/modules/client";
import icpList from '@/utils/ipc'
import tokenInfo from '@/stores/modules/token';

//获取配置
export async function getAllUserConfigs(data){
    return GET('/api/ai_image/camera_machine/get_all_user_configs',data)

}


//更新配置
export async function setAllUserConfigs(data){
    const result = await POST('/api/ai_image/camera_machine/update_all_user_configs',data);

    // 同步到Python
    try {
        const clientStore = client();
        const tokenInfoStore = tokenInfo();
        const token = tokenInfoStore.getToken;
        await clientStore.ipc.invoke(icpList.setting.syncSysConfigs, {
            token: token || ''
        });
    } catch (error) {
        console.error('同步系统配置到Python失败:', error);
        // 同步失败不影响主流程
    }

    return result;
}




//获取左右脚配置
export async function getTopTabs(data){
    return GET('/api/ai_image/camera_machine/get_top_tabs',data)

}

//获取配置下的动作列表
export async function getDeviceConfigs(data){
    return GET('/api/ai_image/camera_machine/get_device_configs',data)

}


//点击切换执行配置
export async function setLeftRightConfig(data){
    const result = await POST('/api/ai_image/camera_machine/update_left_right_config',data);

    // 同步到Python
    try {
        const clientStore = client();
        const tokenInfoStore = tokenInfo();
        const token = tokenInfoStore.getToken;
        await clientStore.ipc.invoke(icpList.setting.syncActions, {
            token: token || '',
            action: 'update',
            data: data
        });
    } catch (error) {
        console.error('同步左右脚配置到Python失败:', error);
        // 同步失败不影响主流程
    }

    return result;
}


//重置配置
export async function restConfig(data){
    const result = await POST('/api/ai_image/camera_machine/reset_config',data);

    // 同步到Python
    try {
        const clientStore = client();
        const tokenInfoStore = tokenInfo();
        const token = tokenInfoStore.getToken;
        await clientStore.ipc.invoke(icpList.setting.syncActions, {
            token: token || '',
            action: 'reset',
            tab_id: data.tab_id
        });
    } catch (error) {
        console.error('同步重置配置到Python失败:', error);
        // 同步失败不影响主流程
    }

    return result;
}



//排序
export async function sortDeviceConfig(data){
    console.log(data);
    const result = await POST('/api/ai_image/camera_machine/sort_device_config',data);

    // 同步到Python
    try {
        const clientStore = client();
        const tokenInfoStore = tokenInfo();
        const token = tokenInfoStore.getToken;
        await clientStore.ipc.invoke(icpList.setting.syncActions, {
            token: token || '',
            action: 'sort',
        });
    } catch (error) {
        console.error('同步重置配置到Python失败:', error);
        // 同步失败不影响主流程
    }

    return result;
}


//更新顶部TOP
export async function setTabName(data){
    const result = await POST('/api/ai_image/camera_machine/update_tab_name',data);

    // 同步到Python
    try {
        const clientStore = client();
        const tokenInfoStore = tokenInfo();
        const token = tokenInfoStore.getToken;
        await clientStore.ipc.invoke(icpList.setting.syncActions, {
            token: token || '',
            action: 'rename',
            id: data.id,
            mode_name: data.mode_name
        });
    } catch (error) {
        console.error('同步重命名配置到Python失败:', error);
        // 同步失败不影响主流程
    }

    return result;
}

//删除可执行命令
export async function delDviceConfig(data){
    const result = await POST('/api/ai_image/camera_machine/remove_device_config',data);

    // 同步到Python
    try {
        const clientStore = client();
        const tokenInfoStore = tokenInfo();
        const token = tokenInfoStore.getToken;
        await clientStore.ipc.invoke(icpList.setting.syncActions, {
            token: token || '',
            action: 'delete',
            id: data.id
        });
    } catch (error) {
        console.error('同步删除配置到Python失败:', error);
        // 同步失败不影响主流程
    }

    return result;
}




//获取设备配置详情
export async function getDeviceConfigDetail(data){
    return GET('/api/ai_image/camera_machine/device_config_detail',data)

}

//根据条件查询可执行程序-单条
export async function getDeviceConfigDetailQuery(data){
    return GET('/api/ai_image/camera_machine/device_config_detail_query',data)

}



//创建或者保存动作
export async function saveDeviceConfig(data){
    const result = await POST('/api/ai_image/camera_machine/save_device_config',data);

    // 同步到Python
    try {
        const clientStore = client();
        const tokenInfoStore = tokenInfo();
        const token = tokenInfoStore.getToken;
        await clientStore.ipc.invoke(icpList.setting.syncActions, {
            token: token || '',
            action: 'save',
            data: data
        });
    } catch (error) {
        console.error('同步保存配置到Python失败:', error);
        // 同步失败不影响主流程
    }

    return result;
}

// 登录后同步数据
export async function syncAfterLogin() {
    try {
        const clientStore = client();
        const tokenInfoStore = tokenInfo();
        const token = tokenInfoStore.getToken;

        if (!token) {
            console.warn('没有token，跳过数据同步');
            return;
        }

        // 同步系统配置
        await clientStore.ipc.invoke(icpList.setting.syncSysConfigs, {
            token: token
        });

        // 同步动作配置
        await clientStore.ipc.invoke(icpList.setting.syncActions, {
            token: token,
            action: 'sync_all'
        });


        // 线上数据同步到本地
        await clientStore.ipc.invoke(icpList.setting.syncSysConfigs, {
            token: token
        });

        console.log('登录后数据同步成功');
    } catch (error) {
        console.error('登录后数据同步失败:', error);
        // 同步失败不影响登录流程
    }
}


