package org.jeecg.modules.fileupload.controller;

import cn.com.ctop.common.module.service.IMaterialImageInfoService;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 素材库工具接口
 *
 * @author jeecg-boot
 */
@RestController
public class FileController {
    @Autowired
    private IMaterialInfoService materialInfoService;

    @Autowired
    private IMaterialImageInfoService materialImageInfoService;




    @RequestMapping("file/check")
    public Map<String, Object> chenckFile(String code, Long projectId) {
        return materialInfoService.checkMaterialInfo(code, projectId);
    }


    @RequestMapping("file/imageCheck")
    public Map<String, Object> imageCheck(String code, String videoId) {
        return materialImageInfoService.checkMaterialInfo(code, videoId);
    }

    @PostMapping("/insertV2")
    public Map<String, Object> insertV2(@RequestBody JSONObject json) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        json.put("userId", user.getId());
        return materialInfoService.correlationUpload(json);
    }

    @PostMapping("/insertV3")
    public Map<String, Object> insertV3(@RequestBody JSONObject data) {
        Map<String, Object> result = new HashMap<>();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Long projectId = data.getLong("projectId");
        JSONArray array = data.getJSONArray("videoArray");
        //校验重复code逻辑
        JSONArray checkArray = materialInfoService.checkArrayCode(array);
        if (null != checkArray && !checkArray.isEmpty()) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject json = array.getJSONObject(i);
                json.put("userId", user.getId());
                json.put("projectId", projectId);
                materialInfoService.correlationUploadV2(json);
            }
        }
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }


    @PostMapping("/insertImage")
    public Map<String, Object> insertImage(@RequestBody JSONObject json) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject()
                .getPrincipal();

        json.put("userId", user.getId());
        return materialInfoService.insertImage(json);
    }


    @PostMapping("file/insert")
    public Map<String, Object> insertFile(@RequestBody JSONObject json) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject()
                .getPrincipal();

        json.put("userId", user.getId());
        return materialInfoService.insert(json);
    }


    @PostMapping("file/batchInsert")
    public Map<String, Object> batchInsert(@RequestBody JSONObject json) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject()
                    .getPrincipal();
            json.put("userId", user.getId());
            JSONArray videoArr = json.getJSONArray("videoArr");
            if (!Check.isNull(videoArr)) {
                json.remove("videoArr");

                for (int i = 0; i < videoArr.size(); i++) {
                    JSONObject video = videoArr.getJSONObject(i);
                    json.put("code", video.getString("code"));
                    json.put("url", video.getString("url"));
                    materialInfoService.insert(json);

                }
            }

            ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_SUCCESS.getCode());

        } catch (Exception e) {
            ResultMapUtils.setResultMap(resultMap, StatusCode.MATERIAL_UPLOAD_FAIL.getCode());
            e.printStackTrace();
        }
        return resultMap;
    }
}
