package com.quinn.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quinn.pojo.*;
import com.quinn.service.BlogService;
import com.quinn.service.CommentService;
import com.quinn.service.StarService;
import com.quinn.service.UserInfoService;
import com.quinn.vo.LayerPhoto;
import com.quinn.vo.LayerPhotoData;
import com.quinn.vo.MyPageParam;
import com.quinn.vo.UserNavReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author limqsh
 * @since 2020-06-28
 */
@Controller
public class UserController extends BaseModelController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    BlogService blogService;
    @Autowired
    CommentService commentService;
    @Resource
    StarService starService;

    @GetMapping("/user")
    public String userIndex(HttpServletRequest request, Model model){
        String uid = getLoginUserId(request);
        // 用户信息回填
        userInfoCallBack(uid,model);
        // 用户的论坛列表
        MyPageParam myPageParam = new MyPageParam(1,10);
        List<BlogWithUser> blogList = blogService.getMyBlogs(uid, myPageParam);
        model.addAttribute("blogList",blogList);
        model.addAttribute("pageParam",myPageParam);
        return "user/index";
    }

    @PostMapping("/user/blog")
    public String userIndexBlog(UserNavReq navReq, Model model){
        // 用户信息回填
        userInfoCallBack(navReq.getUserId(),model);
        // 用户的论坛列表
        if (navReq.getPageNum() < 1){
            navReq.setPageNum(1);
        }
        MyPageParam myPageParam = new MyPageParam(navReq.getPageNum(),navReq.getLimit());
        List<BlogWithUser> blogList = blogService.getMyBlogs(navReq.getUserId(), myPageParam);

        model.addAttribute("blogList",blogList);
        model.addAttribute("pageParam",myPageParam);

        return "user/index::user_table_refresh";
    }

    @GetMapping("/user/comment")
    public String userIndexComment(HttpServletRequest request, Model model){
        String uid = getLoginUserId(request);
        // 用户信息回填
        userInfoCallBack(uid,model);
        // 用户的论坛列表
        Page<Comment> pageParam = new Page<>(1,10);
        commentService.page(pageParam,new QueryWrapper<Comment>().eq("user_id", uid)
                .orderByDesc("gmt_create"));
        // 结果
        List<Comment> commentList = pageParam.getRecords();
        model.addAttribute("commentList",commentList);
        model.addAttribute("pageParam",pageParam);
        return "user/user-comment";
    }

    @PostMapping("/user/comment")
    public String userPageComment(UserNavReq navReq,Model model){
        // 用户信息回填
        userInfoCallBack(navReq.getUserId(),model);
        // 用户的论坛列表
        if (navReq.getPageNum() < 1){
            navReq.setPageNum(1);
        }
        Page<Comment> pageParam = new Page<>(navReq.getPageNum(),navReq.getLimit());
        commentService.page(pageParam,new QueryWrapper<Comment>().eq("user_id", navReq.getUserId())
                .orderByDesc("gmt_create"));
        // 结果
        List<Comment> commentList = pageParam.getRecords();
        model.addAttribute("commentList",commentList);
        model.addAttribute("pageParam",pageParam);
        return "user/user-comment::user_table_refresh";
    }

    @GetMapping("/user/star")
    public String userIndexStar(HttpServletRequest request, Model model){
        String uid = getLoginUserId(request);
        // 用户信息回填
        userInfoCallBack(uid,model);
        // 用户的论坛列表
        MyPageParam myPageParam = new MyPageParam(1,10);
        List<StarWithTopic> starList = starService.listStars(uid, myPageParam);
        // 结果
        model.addAttribute("starList",starList);
        model.addAttribute("pageParam",myPageParam);
        return "user/user-star";
    }

    @PostMapping("/user/star")
    public String userPageStar(UserNavReq navReq,Model model){
        // 用户信息回填
        userInfoCallBack(navReq.getUserId(),model);
        // 用户收藏列表
        if (navReq.getPageNum() < 1){
            navReq.setPageNum(1);
        }
        MyPageParam myPageParam = new MyPageParam(navReq.getPageNum(),navReq.getLimit());
        List<StarWithTopic> starList = starService.listStars(navReq.getUserId(), myPageParam);
        // 结果
        model.addAttribute("starList",starList);
        model.addAttribute("pageParam",myPageParam);
        return "user/user-star::user_table_refresh";
    }

    // 捐赠layer弹窗二维码
    @GetMapping("/user/donate")
    @ResponseBody
    public String userLayerDonate(){
        ArrayList<LayerPhotoData> layerPhotos = new ArrayList<>();

        layerPhotos.add(new LayerPhotoData().setAlt("支付宝").setPid(1).setSrc("/images/donate/alipay.png").setThumb(""));
        layerPhotos.add(new LayerPhotoData().setAlt("微信").setPid(2).setSrc("/images/donate/wechat.jpg").setThumb(""));

        LayerPhoto donate = new LayerPhoto().setTitle("感谢大佬支持").setId(666).setStart(1);
        donate.setData(layerPhotos);

        String donateJsonString = JSONObject.toJSONString(donate);
        return donateJsonString;
    }

    // 用户信息回填
    private void userInfoCallBack(String uid,Model model){
        // 获取用户的发布文章、收藏、评论数
        int blogCount = blogService.count(new QueryWrapper<Blog>().eq("author_id", uid));
        int starCount = starService.count(new QueryWrapper<Star>().eq("user_id", uid));
        int commentCount = commentService.count(new QueryWrapper<Comment>().eq("user_id", uid));
        model.addAttribute("blogCount",blogCount);
        model.addAttribute("starCount",starCount);
        model.addAttribute("commentCount",commentCount);
    }

}

