package com.fuint.module.clientApi.controller;

import com.fuint.common.dto.UserInfo;
import com.fuint.common.service.MerchantService;
import com.fuint.common.service.TaskService;
import com.fuint.common.util.TokenUtil;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
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.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务中心接口 controller
 *
 * Created by Codex
 */
@Api(tags = "会员端-任务中心相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/clientApi/task")
public class ClientTaskController extends BaseController {

    private TaskService taskService;

    private MerchantService merchantService;

    @ApiOperation(value = "任务列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
        String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
        Integer storeId = StringUtil.isEmpty(request.getHeader("storeId")) ? 0 : Integer.parseInt(request.getHeader("storeId"));
        UserInfo userInfo = TokenUtil.getUserInfo();
        Integer userId = userInfo == null ? 0 : userInfo.getId();

        Integer merchantId = merchantService.getMerchantId(merchantNo);
        if (merchantId <= 0) {
            return getFailureResult(201, "商户信息异常");
        }

        List<Map<String, Object>> taskList = taskService.queryClientTaskList(merchantId, storeId, userId);
        int totalDailyPoints = 0;
        BigDecimal totalDailyBalance = BigDecimal.ZERO;
        for (Map<String, Object> task : taskList) {
            int rewardPoint = task.get("rewardPoint") == null ? 0 : Integer.parseInt(task.get("rewardPoint").toString());
            BigDecimal rewardBalance = task.get("rewardBalance") == null ? BigDecimal.ZERO : new BigDecimal(task.get("rewardBalance").toString());
            int dailyLimit = task.get("dailyLimit") == null ? 1 : Integer.parseInt(task.get("dailyLimit").toString());
            totalDailyPoints += rewardPoint * dailyLimit;
            totalDailyBalance = totalDailyBalance.add(rewardBalance.multiply(new BigDecimal(dailyLimit)));
        }

        Map<String, Object> outParams = new HashMap<>();
        outParams.put("taskList", taskList);
        outParams.put("totalDailyPoints", totalDailyPoints);
        outParams.put("totalDailyBalance", totalDailyBalance);
        return getSuccessResult(outParams);
    }

    @ApiOperation(value = "完成任务")
    @RequestMapping(value = "/complete", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject complete(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
        UserInfo userInfo = TokenUtil.getUserInfo();
        if (userInfo == 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 taskId = param.get("taskId") == null ? 0 : Integer.parseInt(param.get("taskId").toString());
        if (taskId <= 0) {
            return getFailureResult(201, "任务ID不能为空");
        }
        Integer merchantId = merchantService.getMerchantId(merchantNo);
        if (merchantId <= 0) {
            return getFailureResult(201, "商户信息异常");
        }

        Map<String, Object> result = taskService.completeTask(merchantId, storeId, userInfo.getId(), taskId, "USER");
        return getSuccessResult(result);
    }

    @ApiOperation(value = "领取任务奖励")
    @RequestMapping(value = "/claim", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject claim(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
        UserInfo userInfo = TokenUtil.getUserInfo();
        if (userInfo == 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 taskId = param.get("taskId") == null ? 0 : Integer.parseInt(param.get("taskId").toString());
        if (taskId <= 0) {
            return getFailureResult(201, "任务ID不能为空");
        }
        Integer merchantId = merchantService.getMerchantId(merchantNo);
        if (merchantId <= 0) {
            return getFailureResult(201, "商户信息异常");
        }

        Map<String, Object> result = taskService.claimTask(merchantId, storeId, userInfo.getId(), taskId, "USER");
        return getSuccessResult(result);
    }
}
