package com.fdkankan.platform.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.platform.common.DownloadStatusEnum;
import com.fdkankan.platform.goods.service.ICameraService;
import com.fdkankan.platform.user.request.RequestUser;
import com.fdkankan.platform.user.service.IUserSceneService;
import com.fdkankan.redis.constant.RedisKey;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.scene.api.feign.SceneUserSceneClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserSceneServiceImpl implements IUserSceneService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    ICameraService cameraService;
    @Autowired
    SceneUserSceneClient sceneUserSceneClient;


    @Override
    public ResultData checkDownload(RequestUser param) {
        if(StringUtils.isEmpty(param.getSceneNum())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        return sceneUserSceneClient.checkDownLoad(param.getSceneNum());
    }

    @Override
    public Map<String, Object> downloadSceneAdmin(RequestUser param, String token) {
        if(StringUtils.isEmpty(param.getSceneNum())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        sceneUserSceneClient.checkDownLoad(param.getSceneNum());
        Map<String, Object> result = new HashMap<>();

        result.put("downloadStatus", 1);

        // 存储redis 开始下载任务
        Map<String,String> params = new HashMap<>(2);
        params.put("sceneNum",param.getSceneNum());
        params.put("type","local");
        redisUtil.lRightPush(RedisKey.DOWNLOAD_TASK,JSONObject.toJSONString(params));
        return result;
    }

    @Override
    public JSONObject getSceneDownloadProcessAdmin(String sceneNum) throws Exception {
        if (StringUtils.isEmpty(sceneNum)) {
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        // 获取下载进度
        String processStr = redisUtil.get(String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS, sceneNum));
        if (StringUtils.isEmpty(processStr)) {
            JSONObject object = new JSONObject();
            object.put("status", 1000);
            object.put("url", "");
            object.put("percent", 0);
            return object;
        }
        JSONObject process = JSONObject.parseObject(processStr);
        switch (process.getInteger("status")) {
            case DownloadStatusEnum.DOWNLOAD_SUCCESS_CODE:
                String url = process.getString("url");
                if (!StringUtils.isEmpty(url)) {
                    break;
                }
                process.put("status", DownloadStatusEnum.DOWNLOAD_FAILED_CODE);
            case DownloadStatusEnum.DOWNLOAD_FAILED_CODE:
                break;
        }
        return process;
    }


}
