/**
 *
 */
package jp.agentec.sinaburocast.csv;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jp.agentec.sinaburocast.common.SinaburoConstant;
import jp.agentec.sinaburocast.entity.Member;
import jp.agentec.sinaburocast.entity.Prefecture;
import jp.agentec.sinaburocast.service.MemberService;
import jp.agentec.sinaburocast.service.PrefectureService;

import org.seasar.framework.util.StringUtil;
import org.seasar.s2csv.csv.annotation.CSVValidator;
import org.seasar.s2csv.csv.annotation.column.CSVColumn;
import org.seasar.s2csv.csv.annotation.column.CSVMask;
import org.seasar.s2csv.csv.annotation.column.CSVMaxLength;
import org.seasar.s2csv.csv.annotation.column.CSVRequired;
import org.seasar.s2csv.csv.annotation.entity.CSVEntity;
import org.seasar.s2csv.csv.annotation.entity.CSVRecordValidator;
import org.seasar.s2csv.csv.exception.runtime.CSVValidationException;
import org.seasar.struts.util.RequestUtil;

/**
 * @author choi-c
 *
 */
@CSVRecordValidator(msgKey="errors.mailCheck", method="validateRecord",after=true)
@CSVEntity(header=true,demiliter='\t',headerCheck=false,columnCountCheck=false)
public class MemberMigrationInfoCsv extends AbstractCsv {

	public PrefectureService prefectureService;
	public MemberService memberService;

	private int checkMemberId;

	@CSVValidator(msgKey = "errors.memberId2", method = "validateMemberId")
	@CSVColumn(columnIndex = 0, columnName = "会員ID")
	public String memberId;

	@CSVRequired
	//@CSVMask(mask = "^[0-9a-zA-Z]{6,20}$")
	@CSVValidator(msgKey = "errors.loginId", method = "validateLoginId")
	@CSVColumn(columnIndex = 2, columnName = "ログイン名")
	public String loginId;

	@CSVRequired
	//@CSVMask(mask = "^[0-9a-zA-Z]{4,20}$")
	@CSVColumn(columnIndex = 3, columnName = "パスワード")
	public String password;

	@CSVMaxLength(maxlength = 30)
	@CSVMask(mask = "^[^0-9]+$")
	@CSVColumn(columnIndex = 4, columnName = "氏")
	public String firstName;

	@CSVMaxLength(maxlength = 30)
	@CSVMask(mask = "^[^0-9]+$")
	@CSVColumn(columnIndex = 5, columnName = "名")
	public String lastName;
	
	@CSVMaxLength(maxlength = 30)
	@CSVMask(mask = "^[ァ-ヶー]+$")
	@CSVColumn(columnIndex = 6, columnName = "氏(カナ)")
	public String firstNameKana;

	@CSVMaxLength(maxlength = 30)
	@CSVMask(mask = "^[ァ-ヶー]+$")
	@CSVColumn(columnIndex = 7, columnName = "名(カナ)")
	public String lastNameKana;
	
	
	@CSVMaxLength(maxlength = 64)
	@CSVValidator(msgKey = "errors.pcEmail", method = "validatepcEmail")
	@CSVMask(mask = "^[!-~]+@[!-~]+$")
	@CSVColumn(columnIndex = 9, columnName = "メールアドレス(PC)")
	public String pcEmail;
	
	@CSVMaxLength(maxlength = 64)
	@CSVValidator(msgKey = "errors.mbEmail", method = "validatembEmail")
	@CSVMask(mask = "^[!-~]+@[!-~]+$")
	@CSVColumn(columnIndex = 10, columnName = "メールアドレス(携帯)")
	public String mbEmail;
	
	@CSVValidator(msgKey = "errors.prefecture", method = "validatezipCode")
	//@CSVMask(mask = "^[0-9]{7}$")
	@CSVColumn(columnIndex = 23, columnName = "郵便番号")
	public String zipCode;
	
	
	@CSVMaxLength(maxlength = 256)
	@CSVValidator(msgKey = "errors.prefecture", method = "validatePrefectureName")
	@CSVColumn(columnIndex = 24, columnName = "都道府県")
	public String prefectureName;
	public Integer prefectureId;
	
	@CSVMaxLength(maxlength = 256)
	@CSVColumn(columnIndex = 25, columnName = "市区町村")
	public String cityName;
	
	@CSVMaxLength(maxlength = 256)
	@CSVColumn(columnIndex = 26, columnName = "番地")
	public String areaName;
	
	@CSVMaxLength(maxlength = 256)
	@CSVColumn(columnIndex = 27, columnName = "建物名")
	public String buildingName;

	//@CSVMask(mask = "^[0-9]{8,20}$")
	@CSVColumn(columnIndex = 28, columnName = "電話")
	public String telNo;
	
	@CSVValidator(msgKey = "errors.prefecture", method = "validateGenderCd")
	@CSVColumn(columnIndex = 19, columnName = "性別")
	public String genderName;//男性・女性
	public Integer genderCd;
	
	@CSVValidator(msgKey = "errors.birthday", method = "validatebirthday")
	@CSVColumn(columnIndex = 20, columnName = "生年月日")
	public String birthdayInput;
	public String birthday;

	
	//@CSVRequired
	@CSVMask(mask = "^[0-9]+$")
	@CSVColumn(columnIndex = 49, columnName = "有効ポイント数")
	public String pointNum;

	@CSVMaxLength(maxlength = 256)
	@CSVColumn(columnIndex = 30, columnName = "メモ")
	public String memo;
	
	public String delFlg = SinaburoConstant.DelFlg.NOT_DEL;
	public Integer validFlg = SinaburoConstant.ValidFlg.VALID;
	public Integer authFailNum=0;
	/**市内フラグ*/
	public Integer localCityFlg;
	
	
	public boolean validateRecord(String[] args) {

		String pcMail = args[7];
		String mbMail = args[8];

		if (StringUtil.isBlank(pcMail) && StringUtil.isBlank(mbMail)) {
			return false;
		}

		return true;
	}

	/**
	 * {@link #versionNo}フィールドのバリデーションを行います。
	 */
	public boolean validatePrefectureName(String arg) {

		if (StringUtil.isBlank(arg))
			return true;

		HttpServletRequest request = RequestUtil.getRequest();

		@SuppressWarnings("unchecked")
		HashMap<String, Integer> postalMap = (HashMap<String, Integer>) request
				.getAttribute("postal");

		if (postalMap == null) {

			List<Prefecture> prefectureInfo = prefectureService
					.findAllOrderById();
			postalMap = new HashMap<String, Integer>();

			// 都道府県
			for (Prefecture data : prefectureInfo) {
				postalMap.put(data.prefecture, data.prefectureId);
			}
			request.setAttribute("postal", postalMap);
		}
		// キーがない場合はエラー
		if (!postalMap.containsKey(arg)) {

			return false;
		}

		prefectureId = postalMap.get(arg);
		return true;
	}

	public boolean validateGenderCd(String args) {

		if (StringUtil.isBlank(args)) {
			genderCd = null;
			return true;
		}

		if (args.equals("男性")) {
			genderCd = SinaburoConstant.GenderType.MAN;
		} else if (args.equals("女性")) {
			genderCd = SinaburoConstant.GenderType.WOMAN;
		} else {
			genderCd = null;
		}
		return true;
	}

	public boolean validatezipCode(String args) {

		if (StringUtil.isBlank(args)) {
			localCityFlg = 0;
			return true;
		}


		if (args.startsWith("272")) {
			localCityFlg = 1;
		} else {
			localCityFlg = 0;
		}
		return true;
	}
	private Date date = null;
	private SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
	
	public boolean validatebirthday(String args) {
		
		if(StringUtil.isBlank(args))
			return true;
		format.setLenient(false);
		
		try {
			date=format.parse(args);
		} catch (ParseException e) {
			return false;
		}
		format = new SimpleDateFormat("yyyyMMdd");
		birthday=format.format(date);
		return true;
	}
	
	
//	public static String dateasa(String args) {
//
//		if(StringUtil.isBlank(args))
//			return "";
//
//		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
//		format.setLenient(false);
//		Date date = null;
//		try {
//			date = format.parse(args);
//		} catch (ParseException e) {
//			return "";
//		}
//		format = new SimpleDateFormat("yyyyMMdd");
//		return format.format(date);
//	}

	
//	public static void main(String[] args) {
//		System.out.println(dateasa("2012/1/1"));
//		System.out.println(dateasa("2012/10/11"));
//		System.out.println(dateasa("2012/10/1"));
//		
//	}
	
	public boolean validatepcEmail(String args) {

		boolean result = false;

		Member memberInfo = null;

		if(StringUtil.isBlank(args)) return true;

		memberInfo = memberService.findByPcEmail(args);

		if (memberInfo != null) {
			if (StringUtil.isBlank(args)) {
				result = false;
				throw new CSVValidationException(
						"errors.pcEmail",
						new Object[]{"costom validation error"}
						);
			}

			if(memberInfo.memberId.equals(checkMemberId)) {
				result = true;
			}
			result = false;
			throw new CSVValidationException(
					"errors.pcEmail",
					new Object[]{"costom validation error"}
					);
		}
		result = true;
//		if (SinaburoUtil.mobileAddressCheck(args)) {
//			result = false;
//			throw new CSVValidationException(
//					"errors.pcEmail1",
//					new Object[]{"costom validation error2"}
//					);
//		} else {
//			result = true;
//		}

		return result;
	}

	public boolean validatembEmail(String args) {

		boolean result = false;

		Member memberInfo = null;

		if(StringUtil.isBlank(args)) return true;

		memberInfo = memberService.findByMoEmail(args);

		if (memberInfo != null) {
			if (StringUtil.isBlank(args)) {
				result = false;
				throw new CSVValidationException(
						"errors.mbEmail",
						new Object[]{"costom validation error"}
						);
			}
			if(memberInfo.memberId.equals(checkMemberId)) {
				result = true;
			}
			result = false;
			throw new CSVValidationException(
					"errors.mbEmail",
					new Object[]{"costom validation error"}
					);
		}

		result = true;

//		if (!SinaburoUtil.mobileAddressCheck(args)) {
//			result = false;
//			throw new CSVValidationException(
//					"errors.mbEmail1",
//					new Object[]{"costom validation error2"}
//					);
//		} else {
//			result = true;
//		}
		
		return result;
	}



	public boolean validateMemberId(String args) {

		if (!StringUtil.equals(args, "")) {
			checkMemberId = Integer.parseInt(args);
		}

		return true;
	}

	public boolean validateLoginId(String args) {

		Member memberInfo = null;

		if(StringUtil.isBlank(args)) return true;

		memberInfo = memberService.findByUserId(args);

		if (memberInfo != null) {
			if (StringUtil.isBlank(args)) {
				return false;
			}
			if(memberInfo.memberId.equals(checkMemberId)) {
				return true;
			}
			return false;
		}
		return true;
	}
	
	
	@CSVColumn(columnIndex = 1,columnName="管理コード")
	public String dummy1;

	@CSVColumn(columnIndex = 8,columnName="メールアドレス")
	public String dummy8;

	@CSVColumn(columnIndex = 11,columnName="メインアドレス")
	public String dummy11;

	@CSVColumn(columnIndex = 12,columnName="モニター属性")
	public String dummy12;

	@CSVColumn(columnIndex = 13,columnName="配信希望フラグ")
	public String dummy13;

	@CSVColumn(columnIndex = 14,columnName="登録日")
	public String dummy14;

	@CSVColumn(columnIndex = 15,columnName="最終更新日")
	public String dummy15;

	@CSVColumn(columnIndex = 16,columnName="法人名")
	public String dummy16;

	@CSVColumn(columnIndex = 17,columnName="部署名")
	public String dummy17;

	@CSVColumn(columnIndex = 18,columnName="役職名")
	public String dummy18;

	@CSVColumn(columnIndex = 21,columnName="年齢")
	public String dummy21;

	@CSVColumn(columnIndex = 22,columnName="年代")
	public String dummy22;

	@CSVColumn(columnIndex = 29,columnName="FAX")
	public String dummy29;

	@CSVColumn(columnIndex = 31,columnName="職業")
	public String dummy31;

	@CSVColumn(columnIndex = 32,columnName="eモニ通信希望フラグ")
	public String dummy32;

	@CSVColumn(columnIndex = 33,columnName="eモニ情報分野")
	public String dummy33;

	@CSVColumn(columnIndex = 34,columnName="(emp)モニターID(メール配信用)")
	public String dummy34;

	@CSVColumn(columnIndex = 35,columnName="プレゼント当選日")
	public String dummy35;

	@CSVColumn(columnIndex = 36,columnName="我が家のニックネーム(えこモニ)")
	public String dummy36;

	@CSVColumn(columnIndex = 37,columnName="えこ帳ファミリーc")
	public String dummy37;

	@CSVColumn(columnIndex = 38,columnName="プレゼント当選日②")
	public String dummy38;

	@CSVColumn(columnIndex = 39,columnName="家族の同居人数")
	public String dummy39;

	@CSVColumn(columnIndex = 40,columnName="同居子供の学齢")
	public String dummy40;

	@CSVColumn(columnIndex = 41,columnName="お住まいの地区はどちらですか")
	public String dummy41;

	@CSVColumn(columnIndex = 42,columnName="環境大臣任命証No")
	public String dummy42;

	@CSVColumn(columnIndex = 43,columnName="同居の高齢者の有無")
	public String dummy43;

	@CSVColumn(columnIndex = 44,columnName="市川市に居住してどのくらいですか")
	public String dummy44;

	@CSVColumn(columnIndex = 45,columnName="市川市e-モニター制度はなんで知りましたか")
	public String dummy45;

	@CSVColumn(columnIndex = 46,columnName="家族構成")
	public String dummy46;

	@CSVColumn(columnIndex = 47,columnName="住居形態")
	public String dummy47;

	@CSVColumn(columnIndex = 48,columnName="環境家計簿モニター(eえこモニ)登録")
	public String dummy48;

//	@CSVColumn(columnIndex = 49,columnName="ポイント(2012/11/13 17:26:00現在)")
//	public String dummy49;
	
	@CSVColumn(columnIndex = 50, columnName = "結果")
	public String okNg = "OK";

	@CSVColumn(columnIndex = 51, columnName = "理由")
	public String reason = "reason";
	
	@CSVColumn(columnIndex = 52, columnName = "登録結果")
	public String insertResult = "OK";

	
	
}