package com.thinkunion.park.service.common.entity;

import com.thinkunion.park.service.common.constant.ParkingConstant;
import com.thinkunion.park.service.common.enums.parking.TicketCauseContent;
import com.thinkunion.park.service.common.utils.JSONPrint;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.tool.utils.DateUtil;
import org.nest.springwrap.core.tool.utils.Func;

import java.util.Date;

/**
 * @author 钱江
 * @description 车辆出场时获取的票根
 * @date 2022/10/1 下午8:19
 * @version 0.3.0
 */
@Data
@Accessors(chain = true)
@Slf4j
public class ExitPassTicket extends PassTicket {

    //停车时长
    protected Integer parkTime;

    //费用金额
    protected Integer amount;

    //是否无感支付
    public Integer isNoFeel;

    protected long supposePlaceIdleCost;

    protected long oldSupposePlaceIdleCost;

    protected long supposeCost;

    protected long placeIdleTime;

    public ExitPassTicket() {
        super();
    }

    public ExitPassTicket(String plateNo, int passStatus, String passMessage, String monthlyCardLed, int billType,
                          int parkTime, int amount) {
        super(plateNo, passStatus, passMessage, monthlyCardLed, billType);
        this.parkTime = parkTime;
        this.amount = amount;
    }

    @Override
    public String getLan1() {
        if (this.isPassForbid()) {
            //无入场时间显示
            if(TicketCauseContent.NOT_IN_EVENT.getValue().equals(this.getPassMessage())){
                return "无入场信息，联系现场保安补录";
            }
            if(Func.isNotEmpty(this.getSpcMessage())&&this.spcMessage.contains("充电车位")){
                return "禁止放行"+this.getSpcMessage();
            }
            return "禁止放行";
        } else {
            if(Func.isNotEmpty(this.getSpcMessage())&&this.spcMessage.contains("充电车位")){
                return this.getSpcMessage();
            }
            return "一路顺风";
        }
    }

    @Override
    public String getLan2() {
        // 判断是否是无牌车
        if (this.isAnonymous()) {
            return this.getPlateNoIfAnonymous();
        }
        return this.getPlateNo();
    }

    @Override
    public String getLan3() {
        if (this.hasSupposePlaceIdleCost()) {
            if (this.isTeam() || this.isMonthlyCard() || this.isSpecial()) {
                return "充电占用时长 " + this.placeIdleTime + " 分钟";
            }
        }

        if (this.isTemporary()) {
            Integer parkTime = 0;
            if (null != this.getParkTime()) {
                parkTime = this.getParkTime();
            }
            return "停车时长：" + parkTime + "分钟";
        } else if (this.isMonthlyCard()) {
            return "月租车辆";
        } else {
            return this.getPassMessage();
        }
    }

    @Override
    public String getLan4() {
        if (this.hasSupposePlaceIdleCost()) {
            if (this.isTeam() || this.isMonthlyCard() || this.isSpecial()) {
                return "需缴占用费 " + this.oldSupposePlaceIdleCost / 100D + " 元";
            }
        }

        if (this.isTemporary()) {

            String content="";

            if(this.isNoFeel==1){
                if(this.supposeCost==0){
                    content="已代扣 -";
                }else{
                    content="需付"+this.supposeCost / 100D+"优惠代扣-"+(this.amount-this.supposeCost)/ 100D+"元;";
                }
            }

            if(this.hasSupposePlaceIdleCost()){
                if((this.amount-this.oldSupposePlaceIdleCost)>0){
                    content+=this.amount / 100D+"元(停车" + (this.amount-this.oldSupposePlaceIdleCost) / 100D + "元,占用费"+oldSupposePlaceIdleCost / 100D+"元)";
                }else{
                    content+=oldSupposePlaceIdleCost / 100D + "元占用费";
                }
            }else{
                content+=(amount-oldSupposePlaceIdleCost) / 100D + "元停车费";
            }


            return content;
        } else if (this.isMonthlyCard()) {
            return this.getMonthlyCardLed();
        } else {
            return "时间: " + DateUtil.formatDateTime(new Date());
        }
    }

    @Override
    public String getVoiceContent() {
        if (this.hasSupposePlaceIdleCost()) {
            if (this.isTeam() || this.isMonthlyCard() || this.isSpecial()) {
                return "需缴占用费 " + this.oldSupposePlaceIdleCost / 100D + " 元";
            }
        }

        String voiceContent = this.getPlateNo().concat("一路顺风");
        if (this.isPassForbid()) {
            voiceContent = this.getPassMessage();
        }
        return voiceContent;
    }

    @Override
    public ExitPassTicket setPlateNo(String plateNo) {
        super.setPlateNo(plateNo);
        return this;
    }

    @Override
    public ExitPassTicket setPassStatus(Integer passStatus) {
        super.setPassStatus(passStatus);
        return this;
    }

    @Override
    public ExitPassTicket setPassMessage(String passMessage) {
        super.setPassMessage(passMessage);
        return this;
    }

    @Override
    public ExitPassTicket setMonthlyCardLed(String monthlyCardLed) {
        super.setMonthlyCardLed(monthlyCardLed);
        return this;
    }

    @Override
    public ExitPassTicket setBillType(Integer billType) {
        super.setBillType(billType);
        return this;
    }

    @Override
    public ExitPassTicket setSubParkingType(Integer subParkingType) {
        super.setSubParkingType(subParkingType);
        return this;
    }

    public ExitPassTicket setSupposeCost(long supposeCost) {
        this.supposeCost = supposeCost;
        if (this.supposeCost > 0) {
            this.passStatus = ParkingConstant.Pass.Forbid;
        }
        return this;
    }

    public void printSelf() {
        log.info("\n\n========================================================================生成离场票根 {} \n lan1 {} " +
                        "\n lan2 {} \n lan3 {} \n lan4 {} \n {} \n" +
                        "===================================================================================\n\n",
                this.getPlateNo(), this.getLan1(),
                this.getLan2(),
                this.getLan3(), this.getLan4(), JSONPrint.printObject(this));
    }

    public boolean hasSupposePlaceIdleCost() {
        return this.supposePlaceIdleCost > 0L;
    }

    public boolean hasSupposeParkingCost() {
        return (this.supposeCost - this.supposePlaceIdleCost) > 0L;
    }
}
