package jp.studyplus.android.sdk.service.studyrecord;

import com.google.common.base.Optional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class StudyRecordBuilder {

	private Optional<String> comment = Optional.absent();
	private Optional<StudyRecordAmount> amount = Optional.absent();
	private Optional<Integer> duration = Optional.absent();
	private Optional<String> recordedTime = Optional.absent();

	public StudyRecordBuilder setComment(String comment){
		this.comment = Optional.of(comment);
		return this;
	}

	public StudyRecordBuilder setAmountTotal(int amount){
		this.amount = Optional.<StudyRecordAmount>of(new AmountTotal(amount));
		return this;
	}

	public StudyRecordBuilder setAmountRange(int start, int end){
		this.amount = Optional.<StudyRecordAmount>of(new AmountRange(start, end));
		return this;
	}

	public StudyRecordBuilder setDurationSeconds(int seconds){
		this.duration = Optional.of(seconds);
		return this;
	}

	public StudyRecordBuilder setRecordedTime(Date date){
		String time = new SimpleDateFormat("yyyy'-'MM'-'dd' 'HH':'mm':'ss", Locale.US).format(date);
		this.recordedTime = Optional.of(time);
		return this;
	}

	public StudyRecordSnapshot build(){
		if (!recordedTime.isPresent()){
			setRecordedTime(new Date());
		}
		if (!comment.isPresent()){
			setComment("");
		}
		if (!duration.isPresent()){
			setDurationSeconds(0);
		}
		return new StudyRecordSnapshotImpl();
	}

	private class StudyRecordSnapshotImpl implements StudyRecordSnapshot {

		@Override
		public String getRecordedTime() {
			return recordedTime.get();
		}

		@Override
		public String getComment() {
			return comment.get();
		}

		@Override
		public int getDuration() {
			return duration.get();
		}

		@Override
		public Optional<StudyRecordAmount> getAmount() {
			return amount;
		}
	}
}

@@ -145,36 +145,103 @@
 til.
-Date;%0Aimport java.util.Local
+Calendar;%0Aimport java.util.GregorianCalendar;%0Aimport java.util.Locale;%0Aimport java.util.TimeZon
 e;%0A%0A
@@ -1098,33 +1098,53 @@
 ime(
-Date date)%7B%0A%09%09String time
+Calendar calendar)%7B%0A%09%09SimpleDateFormat format
  = n
@@ -1210,20 +1210,104 @@
 .US)
-.format(date
+;%0A%09%09format.setTimeZone(calendar.getTimeZone());%0A%09%09String time = format.format(calendar.getTime()
 );%0A%09
@@ -1443,34 +1443,116 @@
 %0A%09%09%09
-setRecordedTime(new Date()
+Calendar current = new GregorianCalendar(TimeZone.getTimeZone(%22UTC%22), Locale.US);%0A%09%09%09setRecordedTime(current
 );%0A%09

fix bug that sends API record-time as JST