package com.fdkankan.platform.user.service;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.platform.common.DownloadStatusEnum;
import com.fdkankan.platform.user.request.RequestUser;
import com.fdkankan.redis.constant.RedisKey;
import com.fdkankan.redis.util.RedisUtil;
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 UserSceneService {

    @Autowired
    private RedisUtil redisUtil;

    public Map<String, Object> checkDownload(RequestUser param) {
        if(StringUtils.isEmpty(param.getSceneNum())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        Map<String, Object> result = new HashMap<>();
        //todo
        return result;
    }

    public Map<String, Object> downloadSceneAdmin(RequestUser param, String token) {
        if(StringUtils.isEmpty(param.getSceneNum())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }

//        ScenePro sceneProEntity = sceneService.findProBySceneNum(param.getSceneNum());
//        if(sceneProEntity == null){
//            throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
//        }
//
//        SceneProEditEntity sceneProEditEntity = sceneProEditService.findByProId(sceneProEntity.getId());
//        if(sceneProEditEntity == null){
//            throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
//        }
        //todo findProBySceneNum  findByProId
        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.set(RedisKey.DOWNLOAD_TASK,JSONObject.toJSONString(params));
        return result;
    }

    public JSONObject getSceneDownloadProcessAdmin(String sceneNum) throws Exception {
        if (StringUtils.isEmpty(sceneNum)) {
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        // 获取下载进度
        String processStr = redisUtil.get(RedisKey.PREFIX_DOWNLOAD_PROGRESS.concat(sceneNum));
        if (StringUtils.isEmpty(processStr)) {
            Map<String, Object> result = new HashMap<>(1);
            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;
    }



}
