package cn.com.ctop.kuaishou.modules.batch.controller;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouImageGet;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouImageGetService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouMaterialUploadService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/kusiShouUpload")
public class KuaiShouMaterialUploadController {

    @Autowired
    private IKuaiShouMaterialUploadService uploadService;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IKuaiShouImageGetService iKuaiShouImageGetService;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;

    @PostMapping(value = "/video")
    public Result<JSONObject> video(@RequestBody JSONObject requestJson) throws Exception {
        Result<JSONObject> result = new Result<>();
        try {
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("请传入账户id");
            }
            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            String url = requestJson.getString("url");
            if (Check.isNull(url)) {
                throw new Exception("请传入视频");
            }
            String videoUrl;
            if (!url.contains("https:")) {
                videoUrl = KuaishouInterfaceConstant.HTTPS_PREFIX + url;
            } else {
                videoUrl = url;
            }
            Integer type = requestJson.getInteger("type");
            if (Check.isNull(type)) {
                throw new Exception("请传入视频类型");
            }
            String signature = requestJson.getString("signature");
            if (Check.isNull(type)) {
                throw new Exception("视频标识错误");
            }
            JSONObject videoJson = uploadService.video(videoUrl, type, accountId, oauthToken.getAccessToken(), signature);
            JSONObject returnJson = new JSONObject();
            if (videoJson.getBoolean("success")) {
                result.setSuccess(true);
                returnJson.put("photoId", videoJson.getString("photoId"));
                returnJson.put("signature", videoJson.getString("signature"));
                returnJson.put("videoUrl", videoUrl);
                result.setResult(returnJson);
                result.setMessage(videoJson.getString("message"));
            } else {
                result.setSuccess(false);
                result.setMessage(videoJson.getString("message"));
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }


        return result;
    }


    @GetMapping(value = "/checkVideo")
    public Result<JSONObject> video(String signature, Long accountId) {
        Result<JSONObject> result = new Result();
        try {
            JSONObject returnJson = new JSONObject();
            QueryWrapper<KuaiShouVideoGet> videoGetQueryWrapper = new QueryWrapper<>();
            videoGetQueryWrapper.eq("signature", signature);
            videoGetQueryWrapper.eq("account_id", accountId);
            videoGetQueryWrapper.last("limit 1");
            KuaiShouVideoGet videoGet = videoGetService.getOne(videoGetQueryWrapper);
            if (Check.isNull(videoGet)) {
                returnJson.put("isExistence", false); // 未存在
            } else {
                returnJson.put("isExistence", true);
                returnJson.put("videoUrl", videoGet.getUrl());
                returnJson.put("photoId", videoGet.getPhotoId());
                returnJson.put("signature", signature);
            }
            result.setSuccess(true);
            result.setResult(returnJson);

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }

        return result;
    }


    @PostMapping(value = "/image")
    public Result<JSONArray> image(@RequestBody JSONObject requestJson) throws Exception {
        Result<JSONArray> result = new Result<>();

        try {
            Long accountId = requestJson.getLong("accountId");
            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            JSONArray returnArr = new JSONArray();
            JSONArray imageArr = requestJson.getJSONArray("uploadImageArr");
            if (!Check.isNull(imageArr)) {
                for (int i = 0; i < imageArr.size(); i++) {
                    JSONObject imageJson = imageArr.getJSONObject(i);
                    String url = imageJson.getString("url");
                    String imageUrl;
                    if (!url.contains("https:")) {
                        imageUrl = KuaishouInterfaceConstant.HTTPS_PREFIX + url;
                    } else {
                        imageUrl = url;
                    }
                    String signature = imageJson.getString("signature");
                    QueryWrapper<KuaiShouImageGet> imageGetQueryWrapper = new QueryWrapper<>();
                    imageGetQueryWrapper.eq("signature", signature);
                    imageGetQueryWrapper.eq("account_id", accountId);
                    imageGetQueryWrapper.last("limit 1");
                    KuaiShouImageGet imageGet = iKuaiShouImageGetService.getOne(imageGetQueryWrapper);
                    if (Check.isNull(imageGet)) {
                        String imageToken = uploadService.kuauiShouImageUpload(imageUrl, signature, accountId, oauthToken.getAccessToken());
                        if (!Check.isNull(imageToken)) {
                            JSONObject returnJson = new JSONObject();
                            returnJson.put("url", imageUrl);
                            returnJson.put("signature", signature);
                            returnArr.add(returnJson);
                        }
                    } else {
                        JSONObject returnJson = new JSONObject();
                        returnJson.put("url", imageUrl);
                        returnJson.put("signature", signature);
                        returnArr.add(returnJson);
                    }
                }

            }
            result.setSuccess(true);
            result.setResult(returnArr);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }

        return result;
    }


}
