package com.gree.mall.miniapp.controller.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.PayDetail;
import com.gree.mall.miniapp.bean.policy.PolicyDetail;
import com.gree.mall.miniapp.bean.policy.PolicyOrderDetail;
import com.gree.mall.miniapp.bean.policy.WorkerRemind;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.bean.worker.WorkerSignResultBean;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.logic.policy.WorkerLogic;
import com.gree.mall.miniapp.plus.entity.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "师傅保险API", tags ={"师傅保险API"} )
@RequestMapping(value = "/worker", produces = "application/json; charset=utf-8")
public class WorkerController {

    @Autowired
    WorkerLogic workerLogic;
    @Autowired
    CommonLogic commonLogic;
    @Autowired
    RedisLockRegistry redisLockRegistry;


    @PostMapping("/remind")
    @ApiOperation(value = "查询师傅是否存在意外险需要提示或购买,1保险购买提示，2意外到期提醒，3意外已到期")
    public ResponseHelper<List<WorkerRemind>> remind(
    ){
        List<WorkerRemind> workerReminds = workerLogic.remind();
        return ResponseHelper.success(workerReminds);
    }


    @PostMapping("/myPolicy")
    @ApiOperation("我的意外保险")
    public ResponseHelper<Page<PolicyOrder>> myPolicy(

            @ApiParam(required = true, value = "pageNum") @RequestParam(required = true) Integer pageNum,
            @ApiParam(required = true, value = "pageSize") @RequestParam(required = true)Integer pageSize
    ){
        Page<PolicyOrder> workerPolicies = workerLogic.myPolicy(pageNum,pageSize);
        return ResponseHelper.success(workerPolicies);
    }

    @PostMapping("/myDetail")
    @ApiOperation("我的保险订单详情")
    public ResponseHelper<PolicyOrderDetail> myDetail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        PolicyOrderDetail policyDetail = workerLogic.myDetail(id);
        return ResponseHelper.success(policyDetail);
    }


    @PostMapping("/myWebsit")
    @ApiOperation("我的续保网点")
    public ResponseHelper<List<AdminWebsit>> myWebsit(
    ){
        List<AdminWebsit> adminWebsitPage = workerLogic.myWebsit();
        return ResponseHelper.success(adminWebsitPage);
    }


    @PostMapping("/agreementList")
    @ApiOperation("保险协议列表")
    public ResponseHelper<List<Agreement>> agreementList(
            @ApiParam(value = "policyId", required = true) @RequestParam String policyId,
            HttpServletRequest request
    ) throws ParseException {
        List<Agreement> agreements =  workerLogic.agreementList(policyId);
        return ResponseHelper.success(agreements);
    }


    @PostMapping("/agreement")
    @ApiOperation("保险协议配置详情")
    public ResponseHelper<Agreement> agreement(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        Agreement agreementDetail = workerLogic.agreement(id);
        return ResponseHelper.success(agreementDetail);
    }


    @PostMapping("/policy")
    @ApiOperation("购买意外保险")
    public ResponseHelper<Page<Policy>> policy(
            @ApiParam(required = true, value = "网点id") @RequestParam(required = true) String websitId,
            @ApiParam(required = true, value = "pageNum") @RequestParam(required = true) Integer pageNum,
            @ApiParam(required = true, value = "pageSize") @RequestParam(required = true)Integer pageSize
    ){
        Page<Policy> workerPolicies = workerLogic.policy(websitId,pageNum,pageSize);
        return ResponseHelper.success(workerPolicies);
    }

    @PostMapping("/detail")
    @ApiOperation("意外保险详情")
    public ResponseHelper<PolicyDetail> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        PolicyDetail policyDetail = workerLogic.detail(id);
        return ResponseHelper.success(policyDetail);
    }

    @PostMapping("/buy")
    @ApiOperation("购买生成订单")
    public ResponseHelper<PolicyOrder> order(
            @ApiParam(value = "保险id", required = true) @RequestParam String policyId,
            @ApiParam(value = "网点id", required = true) @RequestParam String websitId,

            HttpServletRequest request
    ) throws ParseException {
        PolicyOrder order = workerLogic.order(policyId,websitId);
        return ResponseHelper.success(order);
    }


    @PostMapping("/pay")
    @ApiOperation("发起支付")
    public ResponseHelper<PayDetail> pay(
            @ApiParam(value = "订单号", required = true) @RequestParam String policyOrderId,
            HttpServletRequest request
    ) throws ParseException {
        PayDetail payDetail = workerLogic.pay(policyOrderId,request);
        return ResponseHelper.success(payDetail);
    }

    @PostMapping("/ocr/handwriting")
    @ApiOperation(value = "师傅签名手写OCR识别")
    public ResponseHelper<WorkerSignResultBean> ocrHandwriting(
            @ApiParam(required = true, value = "拼接附件") @RequestParam MultipartFile file,
            @ApiParam(required = true, value = "签名附件") @RequestParam MultipartFile signFile,
            @ApiParam(required = true, value = "签名附件base64") @RequestParam String signFile2,
            @ApiParam(required = true, value = "地区 gz=广州 fs=佛山") @RequestParam String type
    ) throws Exception {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + currentCompanyWechat.getUserId());
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("请稍候提交...");
        }
        try {
            WorkerSignResultBean result = workerLogic.ocrHandwriting(file, signFile, signFile2, type);
            return ResponseHelper.success(result);
        } catch(Exception e) {
            log.error("【师傅签名手写OCR识别处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/check/manual/sign/confirm")
    @ApiOperation(value = "检查人工审核师傅签名确认书")
    public ResponseHelper<Boolean> signConfirm(
            @ApiParam(required = true, value = "地区 gz=广州 fs=佛山") @RequestParam String type
    ) throws Exception {
        Boolean result = workerLogic.checkManualSignConfirm(type);
        return ResponseHelper.success(result);
    }

    @PostMapping("/sign/confirm")
    @ApiOperation(value = "师傅签名确认书")
    public ResponseHelper<List<WorkerSignConfirm>> signConfirm(
    ) throws Exception {
        List<WorkerSignConfirm> result = workerLogic.signConfirm();
        return ResponseHelper.success(result);
    }

}
