package com.fuint.module.clientApi.controller;

import com.fuint.common.Constants;
import com.fuint.common.dto.PointDto;
import com.fuint.common.dto.UserInfo;
import com.fuint.common.enums.StatusEnum;
import com.fuint.common.service.GrowService;
import com.fuint.common.service.PointService;
import com.fuint.common.util.DateUtil;
import com.fuint.common.util.TokenUtil;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationRequest;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import com.fuint.repository.model.MtGrow;
import com.fuint.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 积分相关controller
 *
 * Created by FSQ
 * CopyRight https://www.fuint.cn
 */
@Api(tags="会员端-增长相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/clientApi/grow")
public class ClientGrowController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ClientGrowController.class);

    /**
     * 服务接口
     */
    private GrowService growService;

    /**
     * 查询我的积分明细
     *
     * @param request Request对象
     */
    @ApiOperation(value = "查询我的签到")
    @RequestMapping(value = "/sign", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject sign(HttpServletRequest request) throws BusinessCheckException {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return getFailureResult(1001);
        }

        UserInfo mtUser = TokenUtil.getUserInfoByToken(token);
        if (null == mtUser) {
            return getFailureResult(1001);
        };

        MtGrow signInfo = growService.querySignInfo(mtUser.getId());

        Map<String, Object> result = new HashMap<>();
        result.put("growId",signInfo.getId());
        if (signInfo.getSignTime() == null) {
            result.put("isSign",false);
        } else {
            BigDecimal diff = DateUtil.getDiffDays(signInfo.getSignTime(), new Date());
            if (diff.intValue() == 0) {
                result.put("isSign",true);
            } else {
                result.put("isSign",false);
            }
        }

        if (signInfo.getAdvNum() == null) {
            result.put("advNum", 0);
        } else {
            result.put("advNum", signInfo.getAdvNum());
        }
        result.put("advTotal",signInfo.getAdvTotal());
        result.put("growTotal",signInfo.getGrowTotal());
        result.put("growDisp",true);

        return getSuccessResult(result);
    }

    /**
     * 签到
     *
     */
    @ApiOperation(value = "签到")
    @RequestMapping(value = "/doSign", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject doSign(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");
        if (StringUtil.isEmpty(token)) {
            return getFailureResult(1001);
        }

        UserInfo mtUser = TokenUtil.getUserInfoByToken(token);
        if (null == mtUser) {
            return getFailureResult(1001);
        }
        Integer growId = request.getParameter("growId") == null ? 0:Integer.parseInt(request.getParameter("growId"));
logger.info("dosign growid: " + growId);
        try {
            growService.doSign(growId);

            return getSuccessResult(true);

        } catch (BusinessCheckException e) {
            return getFailureResult(3008, "失败");

        }

    }

    /**
     * 点击广告
     *
     */
    @ApiOperation(value = "点击广告")
    @RequestMapping(value = "/doAdv", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject doAdv(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return getFailureResult(1001);
        }
        Integer growId = request.getParameter("growId") == null ? 0:Integer.parseInt(request.getParameter("growId"));

        UserInfo mtUser = TokenUtil.getUserInfoByToken(token);

        if (null == mtUser) {
            return getFailureResult(1001);
        }

        logger.info("doAdv growid: " + growId);

        try {
            growService.doAdv(growId);

            return getSuccessResult(true);

        } catch (BusinessCheckException e) {
            return getFailureResult(3008, "失败");

        }

    }
}
