package com.izouma.zhirongip.dto;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.izouma.zhirongip.annotations.Searchable;
import com.izouma.zhirongip.domain.supply.Patent;
import com.izouma.zhirongip.enums.ApplyStatus;
import com.izouma.zhirongip.enums.CaseType;
import com.izouma.zhirongip.enums.CommissionType;
import com.izouma.zhirongip.utils.DateTimeUtils;
import com.izouma.zhirongip.utils.ObjUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ExcelIgnoreUnannotated
public class PatentExcelDto {

    @ExcelProperty(value = "专利名称")
    private String name;

    @ExcelProperty(value = "申请号")
    private String code;

    @ExcelProperty(value = "专利类型")
    private String patentType;

    @ExcelProperty(value = "申请时间")
    private String applyTime;

    private Long industryClass;

    @ExcelProperty(value = "行业分类")
    private String industryName;

    @ExcelProperty(value = "IPC分类号")
    private String ipc;

    @ExcelProperty(value = "法律状态")
    private String lawStatus;

    @ExcelProperty(value = "申请人")
    private String applicant;

    @ExcelProperty(value = "专利权人")
    private String owner;

    @ExcelProperty(value = "发明人")
    private String inventor;

    @ExcelProperty(value = "摘要")
    private String digest;

    @ExcelProperty(value = "交易方式")
    private String tradingMethod;

    @ExcelProperty(value = "期望价（万元）")
    private String expectedPrice;

    @ExcelProperty(value = "面议")
    private String negotiateDirectly;

    @ExcelProperty(value = "底价（万元）")
    private String basePrice;

    @ExcelProperty(value = "佣金/百分比")
    private String commissionType;

    @ExcelProperty(value = "佣金（万元）")
    private String commission;

    @ExcelProperty(value = "联系人")
    private String contact;

    @ExcelProperty(value = "电话")
    private String phone;

    @ExcelProperty(value = "邮箱")
    private String email;

    @ExcelProperty(value = "所在地区")
    private String address;

    private CaseType caseType = CaseType.GENERAL;

    private ApplyStatus status = ApplyStatus.PASS;

    public static PatentExcelDto from(Patent patent) {
        PatentExcelDto dto = new PatentExcelDto();
        try {
            BeanUtils.copyProperties(dto, patent);
        } catch (Exception e) {
        }
        if (patent.getPatentTypeId() != null) {
            if (7239L == patent.getPatentTypeId()) {
                dto.setPatentType("发明专利");
            } else if (7240L == patent.getPatentTypeId()) {
                dto.setPatentType("实用新型专利");
            } else if (7241L == patent.getPatentTypeId()) {
                dto.setPatentType("外观设计专利");
            }
        }
        if (patent.getApplyTime() != null) {
            dto.setApplyTime(DateTimeUtils.format(patent.getApplyTime(), "yyyyMMdd"));
        }
        if (patent.getLawStatusId() != null) {

            if (7243L == patent.getLawStatusId()) {
                dto.setLawStatus("申请中");
            } else if (7244L == patent.getLawStatusId()) {
                dto.setLawStatus("已授权已期满");
            }
        }
        if (patent.getApplicant() != null) {
            dto.setApplicant(StringUtils.join(patent.getApplicant()));
        }
        if (patent.getOwner() != null) {
            dto.setOwner(StringUtils.join(patent.getOwner()));
        }
        if (patent.getInventor() != null) {
            dto.setInventor(StringUtils.join(patent.getInventor()));
        }
        if (patent.getTradingMethodId() != null) {
            if (7232L == patent.getTradingMethodId()) {
                dto.setTradingMethod("转让");
            } else if (7233L == patent.getTradingMethodId()) {
                dto.setTradingMethod("许可");
            } else if (7234L == patent.getTradingMethodId()) {
                dto.setTradingMethod("独占许可");
            } else if (7235L == patent.getTradingMethodId()) {
                dto.setTradingMethod("排他许可");
            } else if (7236L == patent.getTradingMethodId()) {
                dto.setTradingMethod("入股");
            } else if (7237L == patent.getTradingMethodId()) {
                dto.setTradingMethod("其他");
            }
        }
        if (patent.getExpectedPrice() != null) {
            dto.setExpectedPrice(patent.getExpectedPrice().toString());
        }
        if (patent.getNegotiateDirectly() != null) {
            dto.setNegotiateDirectly(patent.getNegotiateDirectly() ? "是" : "否");
        }
        if (patent.getBasePrice() != null) {
            dto.setBasePrice(patent.getBasePrice().toString());
        }
        if (patent.getCommissionType() != null) {
            dto.setCommissionType(patent.getCommissionType().getDesc());
        }
        if (patent.getCommission() != null) {
            dto.setCommission(patent.getCommission().toString());
        }

        return dto;
    }

    public Patent toPatent() {
        Patent patent = new Patent();
        try {
//            BeanUtils.copyProperties(patent, this);
            org.springframework.beans.BeanUtils.copyProperties(this, patent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotBlank(this.patentType)) {
            if (this.patentType.contains("发明")) {
                patent.setPatentTypeId(7239L);
                patent.setPatentType("发明专利");
            } else if (this.patentType.contains("实用")) {
                patent.setPatentTypeId(7240L);
                patent.setPatentType("实用新型专利");
            } else if (this.patentType.contains("外观")) {
                patent.setPatentTypeId(7241L);
                patent.setPatentType("外观设计专利");
            }
        }
        if (StringUtils.isNotBlank(this.applyTime)) {
            try {
                if (Pattern.matches("\\d{4}-\\d{2}-\\d{2}", this.applyTime)) {
                    patent.setApplyTime(LocalDate.parse(this.applyTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                } else if (Pattern.matches("\\d{4}/\\d{2}/\\d{2}", this.applyTime)) {
                    patent.setApplyTime(LocalDate.parse(this.applyTime, DateTimeFormatter.ofPattern("yyyy/MM/dd")));
                } else if (Pattern.matches("\\d{4}\\.\\d{2}\\.\\d{2}", this.applyTime)) {
                    patent.setApplyTime(LocalDate.parse(this.applyTime, DateTimeFormatter.ofPattern("yyyy.MM.dd")));
                } else if (Pattern.matches("\\d{4}\\d{2}\\d{2}", this.applyTime)) {
                    patent.setApplyTime(LocalDate.parse(this.applyTime, DateTimeFormatter.ofPattern("yyyyMMdd")));
                }
            } catch (Exception e) {
            }
        }
        if (StringUtils.isNotBlank(this.lawStatus)) {
            if (this.lawStatus.contains("申请中")) {
                patent.setLawStatusId(7243L);
                patent.setLawStatus("申请中");
            } else if (this.lawStatus.contains("已授权")) {
                patent.setLawStatusId(7244L);
                patent.setLawStatus("已授权已期满");
            }
        }
        if (StringUtils.isNotBlank(this.applicant)) {
            patent.setApplicant(splitStr(this.applicant));
        }
        if (StringUtils.isNotBlank(this.owner)) {
            patent.setOwner(splitStr(this.owner));
        }
        if (StringUtils.isNotBlank(this.inventor)) {
            patent.setInventor(splitStr(this.inventor));
        }
        if (StringUtils.isNotBlank(this.tradingMethod)) {
            if (this.tradingMethod.contains("转让")) {
                patent.setTradingMethodId(7232L);
                patent.setTradingMethod("转让");
            } else if (this.tradingMethod.contains("普通")) {
                patent.setTradingMethodId(7233L);
                patent.setTradingMethod("普通许可");
            } else if (this.tradingMethod.contains("独占")) {
                patent.setTradingMethodId(7234L);
                patent.setTradingMethod("独占许可");
            } else if (this.tradingMethod.contains("排他")) {
                patent.setTradingMethodId(7235L);
                patent.setTradingMethod("排他许可");
            } else if (this.tradingMethod.contains("入股")) {
                patent.setTradingMethodId(7236L);
                patent.setTradingMethod("入股");
            } else if (this.tradingMethod.contains("其他")) {
                patent.setTradingMethodId(7237L);
                patent.setTradingMethod("其他");
            }
        }
        if (StringUtils.isNotBlank(this.expectedPrice)) {
            patent.setExpectedPrice(new BigDecimal(this.expectedPrice));
        }
        if (StringUtils.isNotBlank(this.negotiateDirectly)) {
            if ("是".equals(this.negotiateDirectly.trim())) {
                patent.setNegotiateDirectly(true);
            } else if ("否".equals(this.negotiateDirectly.trim())) {
                patent.setNegotiateDirectly(false);
            }
        }
        if (StringUtils.isNotBlank(this.basePrice)) {
            patent.setBasePrice(new BigDecimal(this.basePrice));
        }
        if (StringUtils.isNotBlank(this.commissionType)) {
            if (this.commissionType.contains("固定")) {
                patent.setCommissionType(CommissionType.COMMISSION);
            } else if (this.commissionType.contains("百分比")) {
                patent.setCommissionType(CommissionType.PERCENTAGE);
            }
        }
        if (StringUtils.isNotBlank(this.commission)) {
            patent.setCommission(new BigDecimal(this.commission));
        }

        return patent;
    }


    private static List<String> splitStr(String s) {
        if (StringUtils.isBlank(s)) {
            return new ArrayList<>();
        }
        return Arrays.asList(StringUtils.split(s.replaceAll(" ", "")
                                                .replace(";", ",")
                                                .replace("；", ",")
                                                .replace("，", ","), ","));
    }
}