package com.tzy.common.enums;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 *  身份证类型枚举
 */
@Getter
public enum IDCardTypeEnums {

    ID_CARD( "ID_CARD","居民身份证", true , DiallingCodeEnums.CHINA),
    PASSPORT("PASSPORT", "护照" ,false , DiallingCodeEnums.CHINA),
    HK_CARD("HK_CARD", "香港身份证",false , DiallingCodeEnums.HONGKONG) ,
    HK_AM_PASSPORT("HK_AM_PASSPORT", "港澳通行证",false , DiallingCodeEnums.MACAO),
    TW_PASSPORT("TW_PASSPORT", "台胞证",false , DiallingCodeEnums.TAIWAN),
    OTHER("OTHER", "其他",false , DiallingCodeEnums.CHINA),
    UNKNOWN("UNKNOWN", "未知",false , DiallingCodeEnums.CHINA )  //未知
    ;

    private String desc;
    private String type;
    private boolean defult;
    private DiallingCodeEnums diallingCodeEnums;

    IDCardTypeEnums(String type, String desc, boolean defult, DiallingCodeEnums diallingCodeEnums) {
        this.type = type;
        this.desc = desc;
        this.defult = defult;
        this.diallingCodeEnums = diallingCodeEnums;
    }

    public static List<IDCardTypeEnums> list = Arrays.asList(ID_CARD,HK_CARD);

    public static JSONArray getAll(){
    	JSONArray array = new JSONArray();
    	for(IDCardTypeEnums enums : list){
            if(Objects.equals(enums ,UNKNOWN)){
                continue;
            }
    		JSONObject obj = new JSONObject();
    		obj.put("type", enums.getType());
    		obj.put("desc", enums.getDesc());
            obj.put("default", enums.isDefult());
    		array.add(obj);
    	}
    	return array;
    }

    /**
     * @param type
     * @return
     */
    public static JSONObject get(IDCardTypeEnums type){
        for(IDCardTypeEnums enums : IDCardTypeEnums.values()){
            if(Objects.equals(enums, type)){
                JSONObject obj = new JSONObject();
                obj.put("type", enums.getType());
                obj.put("desc", enums.getDesc());
                obj.put("default", enums.isDefult());
                return obj;
            }
        }
        return null;
    }

    public static String getCertTypeByDiallingCode(String diallingCode){
        if(StringUtils.isBlank(diallingCode)){
            return ID_CARD.getType();
        }
    	for(IDCardTypeEnums enums : IDCardTypeEnums.values()){
            if(DiallingCodeEnums.checkEquals(diallingCode, enums.getDiallingCodeEnums())){
                return enums.getType();
            }
    	}
    	return IDCardTypeEnums.UNKNOWN.type;
    }

    public static boolean isIdCard(String type ){
        return Objects.equals(IDCardTypeEnums.ID_CARD.getType(), type);
    }

    public static String getDesc(String type){
        for(IDCardTypeEnums enums : IDCardTypeEnums.values()){
            if(Objects.equals(enums.getType(), type)){
                return enums.getDesc();
            }
        }
        return IDCardTypeEnums.UNKNOWN.getDesc();
    }
}
