import $http from '@/common/request.js';
import config from '@/config';
import store from '@/store';

function getMapInfoHttp(companyId) {
    var url = ''
    if (companyId) {
        url = `/persagy/spaces?companyId=${companyId}`;
    } else {
        url = `/persagy/spaces`;
    }

    return $http({
        url: url,
        method: 'GET'
    })
}

function getSpaceLightsHttp(spaceId, companyId) {
    if (config.featuresVersion == 'experience') {
        let testResult = {
            'result': 'success',
            'content': JSON.stringify([{
                runStatus: 1,
                lampType: '顶灯',
                id: 1
            },
            {
                runStatus: 1,
                lampType: '射灯',
                id: 2
            }
            ])
        };
        return Promise.resolve({
            data: testResult
        })
    } else {
        return $http({
            url: `/persagy/lights/${spaceId}?companyId=${companyId}`,
            method: 'GET'
        })
    }
}

function setSpaceLightHttp(lightId, opened, companyId) {
    // 体验版默认不发请求默认设置成功
    if (config.featuresVersion == 'experience') {
        return Promise.resolve({
            data: JSON.stringify({ 'result': 'success', 'content': [{ 'result': 'success', 'state': 200, 'exeResult': 'success' }] })
        })
    } else {
        return $http({
            url: '/persagy/lightSwitch' + `?onOrOff=${opened}&companyId=${companyId}`,
            method: 'POST',
            data: JSON.stringify([lightId])
        })
    }
}

function getEnvironmentInfoHttp(spaceId, companyId) {
    return $http({
        url: `/persagy/space/environment/${spaceId}?companyId=${companyId}`,
        method: 'GET'
    })
}

function getAirConditionStatusHttp(spaceId, companyId) {
    return $http({
        url: `/persagy/airCondition/${spaceId}?companyId=${companyId}`,
        method: 'GET'
    })
}

function changeAirconditionHttp(spaceId, status, companyId) {
    if (config.featuresVersion == 'experience') {
        return Promise.resolve();
    } else {
        return $http({
            url: `/persagy/temperature/${spaceId}/${status}?companyId=${companyId}`,
            method: 'GET'
        })
    }
}

function getSpaceBytoothHttp(devices, companyId) {
    return $http({
        url: `/persagy/space/device2?companyId=${companyId}`,
        method: 'POST',
        data: JSON.stringify(devices)
    })
}

function checkChangeLightStatusSuccessHttp(params, companyId) {
    // 体验版不控制数据
    if (config.featuresVersion == 'experience') {
        return Promise.resolve({
            data: JSON.stringify({ 'result': 'success', 'content': [{ 'result': 'success', 'state': 200, 'exeResult': 'success' }] })
        })
    } else {
        return $http({
            url: `/persagy/lightQuery?companyId=${companyId}`,
            method: 'POST',
            headers: {
                'content-type': 'application/json'
            },
            data: JSON.stringify(params)
        })
    }
}

function getCustomServerTimeHttp(spaceId) {
    return $http({
        serverSp: config.brsgServer.duoduoUrl,
        url: `/tenantslink/customScene/queryCustom?projectId=${store.state.company.companyConfig.sagacareProjectId}&spaceId=${spaceId}`,
        method: 'GET'
    })
}

function saveCustomServerTimeHttp(params) {
    let projectId = store.state.company.companyConfig.sagacareProjectId;
    return $http({
        serverSp: config.brsgServer.duoduoUrl,
        url: `/tenantslink/customScene/save?projectId=${projectId}`,
        method: 'POST',
        data: JSON.stringify({ ...params, projectId: projectId })
    })
}


function getUserSpaceList(companyId) {
    return $http({
        url: `/space/company/userSpace?companyId=${companyId}`,
        method: 'GET'
    })
}

export {
    getMapInfoHttp,
    getSpaceLightsHttp,
    setSpaceLightHttp,
    getEnvironmentInfoHttp,
    getAirConditionStatusHttp,
    changeAirconditionHttp,
    getSpaceBytoothHttp,
    checkChangeLightStatusSuccessHttp,
    getCustomServerTimeHttp,
    saveCustomServerTimeHttp,
    getUserSpaceList
}