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.demand.PatentDemand;
import com.izouma.zhirongip.enums.ApplyStatus;
import com.izouma.zhirongip.enums.CaseType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

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

    @ExcelProperty(value = "包含关键字")
    private String keyword;

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

    private Long patentTypeId;

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

    @ExcelProperty(value = "专利状态")
    private String patentStatus;

    private Long patentStatusId;

    @ExcelProperty(value = "行业分类")
    private Long industryClass;

    @Searchable
    @ExcelIgnore
    @Column(columnDefinition = "TEXT")
    @ExcelProperty(value = "需求描述")
    private String description;


    @ExcelProperty(value = "转移转化方式")
    private String mode;

    private Long modeId;

    @ExcelProperty(value = "预算")
    private String expectedPrice;

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

    @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 PatentDemandExcelDto from(PatentDemand patent) {
        PatentDemandExcelDto dto = new PatentDemandExcelDto();
        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.getExpectedPrice() != null) {
            dto.setExpectedPrice(patent.getExpectedPrice().toString());
        }
        if (patent.getNegotiateDirectly() != null) {
            dto.setNegotiateDirectly(patent.getNegotiateDirectly() ? "是" : "否");
        }

        return dto;
    }

    public PatentDemand toPatentDemand() {
        PatentDemand patent = new PatentDemand();
        try {
            BeanUtils.copyProperties(patent, this);
        } catch (Exception e) {
        }
        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.patentStatus)) {
            if (this.patentStatus.contains("申请中")) {
                patent.setPatentStatusId(7243L);
                patent.setPatentStatus("申请中");
            } else if (this.patentStatus.contains("已授权")) {
                patent.setPatentStatusId(7244L);
                patent.setPatentStatus("已授权已期满");
            }
        }
        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);
            }
        }

        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("，", ","), ","));
    }
}