package com.fdkankan.ucenter.controller;

import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.DateUtil;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.common.constants.NacosProperty;
import com.fdkankan.ucenter.entity.IncrementType;
import com.fdkankan.ucenter.entity.UserIncrement;
import com.fdkankan.ucenter.service.IIncrementTypeService;
import com.fdkankan.ucenter.service.IUserIncrementService;
import com.fdkankan.ucenter.util.DateUserUtil;
import com.fdkankan.ucenter.vo.request.IncrementParam;
import com.fdkankan.ucenter.vo.response.ResponseUserIncrement;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ucenter/user/increment")
public class IncrementController extends BaseController {

    @Autowired
    IUserIncrementService userIncrementService;
    @Autowired
    IIncrementTypeService incrementTypeService;

    @GetMapping("/getIncrementType")
    public Result getIncrementType(){
        List<IncrementType> list = incrementTypeService.list();
        if("aws".equals(NacosProperty.uploadType)){
            for (IncrementType incrementType : list) {
                incrementType.setCameraCapacity(incrementType.getCameraSpace());
            }
        }
        return Result.success(list);
    }

    @PostMapping("/findIncrementList")
    public Result findIncrementList(@RequestBody IncrementParam param){
        param.setUserName(getUserName());
        return Result.success(userIncrementService.pageList(param));
    }

    /**
     * 绑定权益包
     */
    @PostMapping("/bindCamera")
    public Result bindCamera(@RequestBody IncrementParam param){
        param.setUserName(getUserName());
        userIncrementService.bindCamera(param);
        return Result.success();
    }
    /**
     * 解绑权益包
     */
    @PostMapping("/unbindCamera")
    public Result unbindCamera(@RequestBody IncrementParam param){
        param.setUserName(getUserName());
        userIncrementService.unbindCamera(param);
        return Result.success();
    }

    /**
     * 根据id查询权益信息
     */
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public Result findById(@RequestBody IncrementParam param) throws Exception{

        UserIncrement userIncrementEntity = userIncrementService.getById(param.getId());
        ResponseUserIncrement responseUserIncrement = new ResponseUserIncrement();
        BeanUtils.copyProperties(userIncrementEntity, responseUserIncrement);

        responseUserIncrement.setIncrementStartStr(DateUserUtil.getDayTime(responseUserIncrement.getIncrementStartTime()));
        responseUserIncrement.setIncrementEndStr(DateUserUtil.getDayTime(responseUserIncrement.getIncrementEndTime()));
        responseUserIncrement.setCreateStr(DateUserUtil.getDayTime(responseUserIncrement.getCreateTime()));
        return Result.success(userIncrementEntity);
    }

    @GetMapping("/cancelSubscription/{incrementId}")
    public Result cancelSubscription(@PathVariable Long incrementId){
        userIncrementService.cancelSubscription(incrementId);
        return Result.success();
    }
}
