package com.bonus.core.pay.common.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.bonus.core.account.v3.web.vo.AccOrdBasicsVO;
import com.bonus.core.pay.channel.account.vo.AccPayResultVO;
import com.bonus.core.pay.common.constants.PayStateEnum;
import com.bonus.framework.config.deserializer.DateTimeDeserializer;
import com.bonus.framework.config.serializer.DateTimeSerializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public class ChannelPayVO {
   private Integer code;
   private String msg;
   private Long tradeChannelId;
   private String outTradeNo;
   private BigDecimal amount;
   private BigDecimal realAmount;
   private Integer payType;
   private Integer payChannel;
   private String payParamStr;
   private String qrCodeInfo;
   @JsonSerialize(
      using = DateTimeSerializer.YYYY_MM_DD_HH_MM_SS.class
   )
   @JsonDeserialize(
      using = DateTimeDeserializer.class
   )
   private LocalDateTime payTime;
   private Long custId;
   private String custName;
   private String orgFullName;
   private AccPayResultVO accPayResultVO;
   private JsonNode channelParam;

   public String getMsg() {
      return CharSequenceUtil.isNotEmpty(this.msg) ? this.msg : PayStateEnum.getDesc(this.code);
   }

   public boolean isAsyncPay() {
      return CharSequenceUtil.isNotBlank(this.payParamStr) || CharSequenceUtil.isNotBlank(this.qrCodeInfo);
   }

   public void fillAccOrdBasicsVo(AccOrdBasicsVO accOrdBasicsVO) {
      if (accOrdBasicsVO != null) {
         this.accPayResultVO = AccPayResultVO.of(accOrdBasicsVO);
      }

   }

   public static ChannelPayVO of(MobilePayVO payVO) {
      ChannelPayVO resultVO = new ChannelPayVO();
      if (PayStateEnum.isPaidState(payVO.getCode())) {
         resultVO.setPayTime(LocalDateTime.now());
      }

      BeanUtil.copyProperties(payVO, resultVO, new String[0]);
      return resultVO;
   }

   public static ChannelPayVO of(AndroidPayVO payVO) {
      ChannelPayVO resultVO = new ChannelPayVO();
      if (PayStateEnum.isPaidState(payVO.getCode())) {
         resultVO.setPayTime(LocalDateTime.now());
      }

      BeanUtil.copyProperties(payVO, resultVO, new String[0]);
      return resultVO;
   }

   public static ChannelPayVO of(OfflinePayVO payVO) {
      ChannelPayVO resultVO = new ChannelPayVO();
      if (PayStateEnum.isPaidState(payVO.getCode())) {
         resultVO.setPayTime(LocalDateTime.now());
      }

      BeanUtil.copyProperties(payVO, resultVO, new String[0]);
      return resultVO;
   }

   public static ChannelPayVO of(QrCodeApplyVO payVO) {
      ChannelPayVO resultVO = new ChannelPayVO();
      BeanUtil.copyProperties(payVO, resultVO, new String[0]);
      if (PayStateEnum.isPaidState(payVO.getCode())) {
         resultVO.setPayTime(LocalDateTime.now());
      }

      return resultVO;
   }

   public Integer getCode() {
      return this.code;
   }

   public Long getTradeChannelId() {
      return this.tradeChannelId;
   }

   public String getOutTradeNo() {
      return this.outTradeNo;
   }

   public BigDecimal getAmount() {
      return this.amount;
   }

   public BigDecimal getRealAmount() {
      return this.realAmount;
   }

   public Integer getPayType() {
      return this.payType;
   }

   public Integer getPayChannel() {
      return this.payChannel;
   }

   public String getPayParamStr() {
      return this.payParamStr;
   }

   public String getQrCodeInfo() {
      return this.qrCodeInfo;
   }

   public LocalDateTime getPayTime() {
      return this.payTime;
   }

   public Long getCustId() {
      return this.custId;
   }

   public String getCustName() {
      return this.custName;
   }

   public String getOrgFullName() {
      return this.orgFullName;
   }

   public AccPayResultVO getAccPayResultVO() {
      return this.accPayResultVO;
   }

   public JsonNode getChannelParam() {
      return this.channelParam;
   }

   public void setCode(final Integer code) {
      this.code = code;
   }

   public void setMsg(final String msg) {
      this.msg = msg;
   }

   public void setTradeChannelId(final Long tradeChannelId) {
      this.tradeChannelId = tradeChannelId;
   }

   public void setOutTradeNo(final String outTradeNo) {
      this.outTradeNo = outTradeNo;
   }

   public void setAmount(final BigDecimal amount) {
      this.amount = amount;
   }

   public void setRealAmount(final BigDecimal realAmount) {
      this.realAmount = realAmount;
   }

   public void setPayType(final Integer payType) {
      this.payType = payType;
   }

   public void setPayChannel(final Integer payChannel) {
      this.payChannel = payChannel;
   }

   public void setPayParamStr(final String payParamStr) {
      this.payParamStr = payParamStr;
   }

   public void setQrCodeInfo(final String qrCodeInfo) {
      this.qrCodeInfo = qrCodeInfo;
   }

   @JsonDeserialize(
      using = DateTimeDeserializer.class
   )
   public void setPayTime(final LocalDateTime payTime) {
      this.payTime = payTime;
   }

   public void setCustId(final Long custId) {
      this.custId = custId;
   }

   public void setCustName(final String custName) {
      this.custName = custName;
   }

   public void setOrgFullName(final String orgFullName) {
      this.orgFullName = orgFullName;
   }

   public void setAccPayResultVO(final AccPayResultVO accPayResultVO) {
      this.accPayResultVO = accPayResultVO;
   }

   public void setChannelParam(final JsonNode channelParam) {
      this.channelParam = channelParam;
   }

}
