package com.yonge.toolset.utils.idcard;

import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import com.yonge.toolset.base.exception.UtilException;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class IdcardInfoExtractor {
	// 省份
	private String province;

	private String provinceCode;

	// 城市
	private String cityCode;

	private String city;

	// 区县
	private String regionCode;

	private String region;

	// 性别(1,男 0,女)
	private int gender;

	// 出生日期
	private Date birthday;

	private static Properties idCardCodeTable;

	static {
		if (idCardCodeTable == null) {
			idCardCodeTable = new Properties();
		}
		try {
            URL resource = Thread.currentThread().getContextClassLoader().getResource("idcardCodeTable.properties");
            idCardCodeTable.load(resource.openStream());
		} catch (IOException e) {
			throw new UtilException("银行卡信息查询接口异常", e);
		}
	}

	private IdcardValidator validator = null;

	public IdcardInfoExtractor(){}

	/**
	 * 通过构造方法初始化各个成员属性
	 */
	public IdcardInfoExtractor(String idcard, boolean isCheck) {
		validator = new IdcardValidator();
		if (isCheck) {
			if (validator.isValidatedAllIdcard(idcard) == false) {
				return;
			}
		}
		if (idcard.length() == 15) {
			idcard = validator.convertIdcarBy15bit(idcard);
		}
		// 获取省份
		provinceCode = idcard.substring(0, 2) + "0000";
		province = idCardCodeTable.getProperty(provinceCode);

		cityCode = idcard.substring(0, 4) + "00";
		city = idCardCodeTable.getProperty(cityCode);

		regionCode = idcard.substring(0, 6);
		region = idCardCodeTable.getProperty(regionCode);

		// 获取性别
		String id17 = idcard.substring(16, 17);
		if (Integer.parseInt(id17) % 2 == 0) {
			this.gender = 0;
		} else {
			this.gender = 1;
		}

		// 获取出生日期
		String birthday = idcard.substring(6, 14);
		Date birthdate = null;
		try {
			birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
		} catch (ParseException e) {
			throw new RuntimeException("身份证信息错误");
		}
		this.birthday = birthdate;

	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getProvinceCode() {
		return provinceCode;
	}

	public void setProvinceCode(String provinceCode) {
		this.provinceCode = provinceCode;
	}

	public String getCityCode() {
		return cityCode;
	}

	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getRegionCode() {
		return regionCode;
	}

	public void setRegionCode(String regionCode) {
		this.regionCode = regionCode;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public int getGender() {
		return gender;
	}

	public void setGender(int gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public IdcardValidator getValidator() {
		return validator;
	}

	public void setValidator(IdcardValidator validator) {
		this.validator = validator;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}


}
