package com.mcwl.web.controller.memberCenter;


import cn.hutool.core.bean.BeanUtil;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.memberCenter.domain.Member;
import com.mcwl.memberCenter.domain.MemberPromotion;
import com.mcwl.memberCenter.domain.Promotion;
import com.mcwl.memberCenter.domain.dto.JoinPromotionDto;
import com.mcwl.memberCenter.domain.dto.PromotionDto;
import com.mcwl.memberCenter.enums.MemberEnum;
import com.mcwl.memberCenter.enums.PromotionEnum;
import com.mcwl.memberCenter.service.MemberPromotionService;
import com.mcwl.memberCenter.service.MemberService;
import com.mcwl.memberCenter.service.PromotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * 促销活动
 */
@RestController
@RequestMapping("promotion")
@RequiredArgsConstructor
@Api(tags = "会员中心")
public class PromotionController {

    private final PromotionService promotionService;

    private final MemberPromotionService memberPromotionService;


    /**
     * 创建活动
     */
//    @PreAuthorize("@ss.hasPermi('system:promotion:add')")
    @PostMapping("createPromotion")
    @ApiOperation(value = "创建活动")
    public R<Object> createPromotion(@RequestBody @Valid PromotionDto promotionDto) {

        Date startTime = promotionDto.getStartTime();
        Date endTime = promotionDto.getEndTime();
        if (startTime.after(endTime)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"活动开始时间不能大于结束时间");
        }
        Promotion promotion = new Promotion();

        BeanUtil.copyProperties(promotionDto, promotion);

        promotionService.save(promotion);

        return R.ok();
    }


    /**
     * 活动列表
     */
    @PostMapping("promotionList")
    @ApiOperation(value = "活动列表")
    public TableDataInfo getPromotionList(@Valid @RequestBody PageDomain pageDomain) {

        return promotionService.getPromotionList(pageDomain);
    }

    /**
     * 获取当前用户参与的活动
     */
    @PostMapping("myPromotionList")
    @ApiOperation(value = "获取当前用户参与的活动")
    public TableDataInfo myPromotionList(@Valid @RequestBody PageDomain pageDomain) {

        return promotionService.getMyPromotionList(pageDomain);
    }

    /**
     * 参与活动
     */
    @GetMapping("joinPromotion")
    @ApiOperation(value = "参与活动")
    public R<Object> joinPromotion(@Valid
                                   @NotNull(message = "活动ID不能为空")
                                   @ApiParam("活动ID")
                                   Long promotionId) {

        return promotionService.joinPromotion(promotionId);
    }

    /**
     * 修改活动
     */
//    @PostMapping("updatePromotion")
//    @ApiOperation(value = "修改活动")
//    public R<Object> updatePromotion(@RequestBody @Valid PromotionDto promotionDto) {
//
//        return promotionService.updatePromotion(promotionDto);
//    }

    /**
     * 删除活动
     */
    @GetMapping("deletePromotion")
    @ApiOperation(value = "删除活动")
    public R<Object> deletePromotion(@Valid
                                     @NotNull(message = "活动ID不能为空")
                                     @ApiParam("活动ID")
                                     Long promotionId) {
        promotionService.removeById(promotionId);
        return R.ok();
    }

//    private boolean isSubscribe(Long userId, Promotion promotion) {
//        if (promotion.getActivityType() == PromotionEnum.SUBSCRIBE) {
//            // 获取当前用户最新的订阅或续订
//            Member member = memberService.latestSubscription(userId);
//            if (!Optional.ofNullable(member).isPresent()) {
//                return false;
//            }
//            Date createTime = member.getCreateTime();
//            // 会员创建时间在活动开始时间之前，说明用户不符合“活动期间内订阅或续订会员”的条件
//            return !createTime.before(promotion.getStartTime());
//        }
//        return true;
//    }


}
