package com.fuint.module.clientApi.controller;

import com.fuint.common.dto.UserInfo;
import com.fuint.common.service.LookService;
import com.fuint.common.service.MerchantService;
import com.fuint.common.util.TokenUtil;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import com.fuint.repository.model.MtLookWork;
import com.fuint.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 看看功能会员端 controller
 *
 * Created by Codex
 */
@Api(tags = "会员端-看看作品相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/clientApi/look")
public class ClientLookController extends BaseController {

    private LookService lookService;

    private MerchantService merchantService;

    @ApiOperation(value = "看看作品列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject list(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
        String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
        Integer storeId = StringUtil.isEmpty(request.getHeader("storeId")) ? 0 : Integer.parseInt(request.getHeader("storeId"));
        Integer merchantId = merchantService.getMerchantId(merchantNo);
        if (merchantId <= 0) {
            return getFailureResult(201, "商户信息异常");
        }

        Integer page = params.get("page") == null ? 1 : Integer.parseInt(params.get("page").toString());
        Integer pageSize = params.get("pageSize") == null ? 10 : Integer.parseInt(params.get("pageSize").toString());
        String mineFlag = params.get("mine") == null ? "0" : params.get("mine").toString();
        Boolean mine = "1".equals(mineFlag) || "true".equalsIgnoreCase(mineFlag);

        UserInfo loginInfo = TokenUtil.getUserInfo();
        Integer userId = loginInfo == null ? 0 : loginInfo.getId();
        if (mine && userId <= 0) {
            return getFailureResult(1001);
        }

        PaginationResponse<Map<String, Object>> paginationResponse = lookService.queryWorkListForClient(
                merchantId,
                storeId,
                userId,
                page,
                pageSize,
                mine
        );

        Map<String, Object> result = new HashMap<>();
        result.put("content", paginationResponse.getContent());
        result.put("pageSize", paginationResponse.getPageSize());
        result.put("pageNumber", paginationResponse.getCurrentPage());
        result.put("totalRow", paginationResponse.getTotalElements());
        result.put("totalPage", paginationResponse.getTotalPages());
        return getSuccessResult(result);
    }

    @ApiOperation(value = "看看作品详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject detail(@RequestBody Map<String, Object> params) throws BusinessCheckException {
        Integer workId = params.get("workId") == null ? 0 : Integer.parseInt(params.get("workId").toString());
        if (workId <= 0) {
            return getFailureResult(201, "作品ID不能为空");
        }

        UserInfo loginInfo = TokenUtil.getUserInfo();
        Integer userId = loginInfo == null ? 0 : loginInfo.getId();
        Map<String, Object> workInfo = lookService.getWorkDetail(workId, userId);

        Map<String, Object> result = new HashMap<>();
        result.put("workInfo", workInfo);
        return getSuccessResult(result);
    }

    @ApiOperation(value = "发布作品")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject save(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
        UserInfo loginInfo = TokenUtil.getUserInfo();
        if (loginInfo == null) {
            return getFailureResult(1001);
        }
        String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
        Integer storeId = StringUtil.isEmpty(request.getHeader("storeId")) ? 0 : Integer.parseInt(request.getHeader("storeId"));
        Integer merchantId = merchantService.getMerchantId(merchantNo);
        if (merchantId <= 0) {
            return getFailureResult(201, "商户信息异常");
        }

        String title = params.get("title") == null ? "" : params.get("title").toString();
        String desc = params.get("desc") == null ? "" : params.get("desc").toString();
        List<String> images = new ArrayList<>();
        if (params.get("images") instanceof List) {
            images = (List<String>) params.get("images");
        }

        MtLookWork work = lookService.saveWork(merchantId, storeId, loginInfo.getId(), title, desc, images, String.valueOf(loginInfo.getId()));
        Map<String, Object> result = new HashMap<>();
        result.put("id", work.getId());
        return getSuccessResult(result);
    }

    @ApiOperation(value = "删除我的作品")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject remove(@RequestBody Map<String, Object> params) throws BusinessCheckException {
        UserInfo loginInfo = TokenUtil.getUserInfo();
        if (loginInfo == null) {
            return getFailureResult(1001);
        }
        Integer workId = params.get("workId") == null ? 0 : Integer.parseInt(params.get("workId").toString());
        if (workId <= 0) {
            return getFailureResult(201, "作品ID不能为空");
        }
        lookService.removeWork(workId, loginInfo.getId(), String.valueOf(loginInfo.getId()));
        return getSuccessResult(true);
    }

    @ApiOperation(value = "评论列表")
    @RequestMapping(value = "/commentList", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject commentList(@RequestBody Map<String, Object> params) throws BusinessCheckException {
        Integer workId = params.get("workId") == null ? 0 : Integer.parseInt(params.get("workId").toString());
        Integer page = params.get("page") == null ? 1 : Integer.parseInt(params.get("page").toString());
        Integer pageSize = params.get("pageSize") == null ? 20 : Integer.parseInt(params.get("pageSize").toString());
        if (workId <= 0) {
            return getFailureResult(201, "作品ID不能为空");
        }

        UserInfo loginInfo = TokenUtil.getUserInfo();
        Integer userId = loginInfo == null ? 0 : loginInfo.getId();

        PaginationResponse<Map<String, Object>> paginationResponse = lookService.queryCommentList(workId, page, pageSize, userId);
        Map<String, Object> result = new HashMap<>();
        result.put("content", paginationResponse.getContent());
        result.put("pageSize", paginationResponse.getPageSize());
        result.put("pageNumber", paginationResponse.getCurrentPage());
        result.put("totalRow", paginationResponse.getTotalElements());
        result.put("totalPage", paginationResponse.getTotalPages());
        return getSuccessResult(result);
    }

    @ApiOperation(value = "发布评论")
    @RequestMapping(value = "/commentSave", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject commentSave(@RequestBody Map<String, Object> params) throws BusinessCheckException {
        UserInfo loginInfo = TokenUtil.getUserInfo();
        if (loginInfo == null) {
            return getFailureResult(1001);
        }

        Integer workId = params.get("workId") == null ? 0 : Integer.parseInt(params.get("workId").toString());
        String content = params.get("content") == null ? "" : params.get("content").toString();
        List<String> images = new ArrayList<>();
        if (params.get("images") instanceof List) {
            images = (List<String>) params.get("images");
        }
        if (workId <= 0) {
            return getFailureResult(201, "作品ID不能为空");
        }

        Map<String, Object> commentInfo = lookService.saveComment(workId, loginInfo.getId(), content, images, String.valueOf(loginInfo.getId()));
        Map<String, Object> result = new HashMap<>();
        result.put("commentInfo", commentInfo);
        return getSuccessResult(result);
    }

    @ApiOperation(value = "作品点赞/取消点赞")
    @RequestMapping(value = "/toggleLike", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject toggleLike(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
        UserInfo loginInfo = TokenUtil.getUserInfo();
        if (loginInfo == null) {
            return getFailureResult(1001);
        }
        Integer workId = params.get("workId") == null ? 0 : Integer.parseInt(params.get("workId").toString());
        if (workId <= 0) {
            return getFailureResult(201, "作品ID不能为空");
        }

        String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
        Integer storeId = StringUtil.isEmpty(request.getHeader("storeId")) ? 0 : Integer.parseInt(request.getHeader("storeId"));
        Integer merchantId = merchantService.getMerchantId(merchantNo);

        Map<String, Object> result = lookService.toggleWorkLike(workId, loginInfo.getId(), merchantId, storeId);
        return getSuccessResult(result);
    }

    @ApiOperation(value = "评论点赞/取消点赞")
    @RequestMapping(value = "/toggleCommentLike", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject toggleCommentLike(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
        UserInfo loginInfo = TokenUtil.getUserInfo();
        if (loginInfo == null) {
            return getFailureResult(1001);
        }
        Integer commentId = params.get("commentId") == null ? 0 : Integer.parseInt(params.get("commentId").toString());
        if (commentId <= 0) {
            return getFailureResult(201, "评论ID不能为空");
        }

        String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
        Integer storeId = StringUtil.isEmpty(request.getHeader("storeId")) ? 0 : Integer.parseInt(request.getHeader("storeId"));
        Integer merchantId = merchantService.getMerchantId(merchantNo);

        Map<String, Object> result = lookService.toggleCommentLike(commentId, loginInfo.getId(), merchantId, storeId);
        return getSuccessResult(result);
    }
}
