package com.kinancity.api.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * Information about a PTC account
 * 
 * @author drallieiv
 *
 */
@Getter
public class AccountData implements Cloneable {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Setter
	private String username;

	@Setter
	private String email;

	@Setter
	private String password;

	// String Date of birth as YYYY-MM-DD
	private String dob = "1985-01-16";

	private String country = "US";

	private SimpleDateFormat dobFormat = new SimpleDateFormat("yyyy-MM-dd");

	public AccountData() {

	}

	public AccountData(String username, String email, String password) {
		this.username = username;
		this.email = email;
		this.password = password;
	}

	public AccountData(String username, String email, String password, String dob, String country) {
		this.username = username;
		this.email = email;
		this.password = password;
		this.dob = dob;
		this.country = country;
	}

	public void setDob(String dob) {
		try {
			if (dobFormat.parse(dob) != null) {
				this.dob = dob;
			}
		} catch (ParseException e) {
			logger.error("Invalid date {}, keep default date", dob);
		}

	}

	public void setCountry(String country) {
		// TODO we should check that this country is supported by PTC
		this.country = country;
	}

	public AccountData clone() {
		return new AccountData(username, email, password, dob, country);
	}

	public String toCsv() {
		return username + ";" + password + ";" + email + ";" + dob + ";" + country;
	}

	@Override
	public String toString() {
		return "AccountData [" + (username != null ? "username=" + username + ", " : "") + (email != null ? "email=" + email + ", " : "") + (password != null ? "password=" + password + ", " : "") + (dob != null ? "dob=" + dob + ", " : "") + (country != null ? "country=" + country : "") + "]";
	}

}

@@ -94,16 +94,41 @@
 eFormat;
+%0Aimport java.util.Random;
 %0A%0Aimport
@@ -226,32 +226,8 @@
 ter;
-%0Aimport lombok.ToString;
 %0A%0A/*
@@ -265,17 +265,16 @@
 count%0A *
- 
 %0A * @aut
@@ -581,23 +581,8 @@
  dob
- = %221985-01-16%22
 ;%0A%0A%09
@@ -712,17 +712,54 @@
 ata() %7B%0A
+%09%09this(null, null, null, null, null);
 %0A
-
 %09%7D%0A%0A%09pub
@@ -826,33 +826,33 @@
 rd) %7B%0A%09%09this
-.
+(
 username
  = username;
@@ -843,69 +843,38 @@
 name
- = username;%0A%09%09this.email = email;%0A%09%09this.password = password
+, email, password, null, null)
 ;%0A%09%7D
@@ -1053,30 +1053,69 @@
 word;%0A%09%09
-this.
+setDob(
 dob 
+!
 = 
-dob
+null ? dob : chooseRandomDateOfBirth())
 ;%0A%09%09this
@@ -1723,24 +1723,24 @@
 oString() %7B%0A
-
 %09%09return %22Ac
@@ -2019,10 +2019,514 @@
 %5D%22;%0A%09%7D%0A%0A
+%09private String chooseRandomDateOfBirth() %7B%0A%09%09Random generator = new Random();%0A%09%09// year: 1970 to 2000%0A%09%09int y = generator.nextInt(30) + 1970;%0A%09%09// month: 1 to 12%0A%09%09int m = generator.nextInt(12) + 1;%0A%09%09// day: depending on month%0A%09%09int maxDays;%0A%09%09switch (m) %7B%0A%09%09%09case 2:%0A%09%09%09%09maxDays = 28;%0A%09%09%09%09break;%0A%09%09%09case 4:%0A%09%09%09case 6:%0A%09%09%09case 9:%0A%09%09%09case 11:%0A%09%09%09%09maxDays = 30;%0A%09%09%09%09break;%0A%09%09%09default:%0A%09%09%09%09maxDays = 31;%0A%09%09%7D%0A%09%09int d = generator.nextInt(maxDays) + 1;%0A%09%09return String.format(%22%2504d-%2502d-%2502d%22, y, m, d);%0A%09%7D%0A%0A
 %7D%0A

Choose a random date of birth