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

import cn.com.ctop.kuaishou.modules.graphql.entity.KuaishouUserCookie;
import cn.com.ctop.kuaishou.modules.graphql.service.IKuaishouUserCookieService;
import cn.com.ctop.kuaishou.modules.graphql.service.IKuaishouWebInterfaceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/ks/web")
public class KuaishouWebController {
    @Autowired
    private IKuaishouWebInterfaceService kuaishouWebInterfaceService;
    @Autowired
    private IKuaishouUserCookieService kuaishouUserCookieService;

    @PostMapping(value = "/login/qr")
    public Result<Map<String, Object>> getQrCode() {
        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            Map<String, Object> resultMap = new HashMap<>();
            String requestId = UUID.randomUUID().toString();
            String img = kuaishouWebInterfaceService.qrLogin(requestId);
            resultMap.put("requestId", requestId);
            resultMap.put("qrcode", "data:image/png;base64," + img);
            if (img != null) {
                result.setSuccess(true);
                result.setResult(resultMap);
            } else {
                result.setSuccess(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping(value = "/user/list")
    public Result<List<KuaishouUserCookie>> getUserCookieList() {
        Result<List<KuaishouUserCookie>> result = new Result<>();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<KuaishouUserCookie> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        queryWrapper.select("ksid", "user_name");
        List<KuaishouUserCookie> list = kuaishouUserCookieService.list(queryWrapper);
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }



    @PostMapping(value = "/login/status")
    public Result<Map<String, Object>> getStatus(String requestId) {
        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            Map<String, Object> statusMap = kuaishouWebInterfaceService.loginStatus(requestId);
            result.setSuccess(true);
            result.setResult(statusMap);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping(value = "/video/list")
    public Result<Map<String, Object>> videoList(String ksid, String pcursor) {
        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            Map<String, Object> statusMap = kuaishouWebInterfaceService.videoList(user.getId(), ksid, pcursor);
            result.setSuccess(true);
            result.setResult(statusMap);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping(value = "/comment/list")
    public Result<Map<String, Object>> commentList(String ksid, String photoId, String pcursor) {
        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            Map<String, Object> statusMap = kuaishouWebInterfaceService.commentList(user.getId(), ksid, photoId, pcursor);
            result.setSuccess(true);
            result.setResult(statusMap);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping(value = "/comment/delete")
    public Result<Map<String, Object>> commentDelete(String ksid, String photoId, Long commentId) {
        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            Map<String, Object> statusMap = kuaishouWebInterfaceService.commentDelete(user.getId(), ksid, photoId, commentId);
            result.setSuccess(true);
            result.setResult(statusMap);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping(value = "/comment/add")
    public Result<Map<String, Object>> commentAdd(String ksid, String photoId, String content, Long replyToCommentId, Long repltTo) {
        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            Map<String, Object> statusMap = kuaishouWebInterfaceService.commentAdd(user.getId(), ksid, photoId, content, replyToCommentId, repltTo);
            result.setSuccess(true);
            result.setResult(statusMap);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping(value = "/subcomment/list")
    public Result<Map<String, Object>> subCommentList(String ksid, String photoId, Long rootCommentId, String pcursor) {
        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            Map<String, Object> statusMap = kuaishouWebInterfaceService.subCommentList(user.getId(), ksid, photoId, rootCommentId, pcursor);
            result.setSuccess(true);
            result.setResult(statusMap);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }
}
