package models;

import java.io.*;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.persistence.*;

import com.avaje.ebean.Model;
import com.avaje.ebean.Model.Finder;
import com.fasterxml.jackson.annotation.*;

import controllers.Application;

import play.libs.Json;

@Entity
public class AttendanceDay extends Model {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "attendance_day_id_seq")
    public Integer id;

    public Date day;

    @ManyToOne()
    @JoinColumn(name="person_id")
    public Person person;

    public String code;

    public Time start_time;
    public Time end_time;

    public static Finder<Integer, AttendanceDay> find = new Finder<Integer, AttendanceDay>(
        AttendanceDay.class
    );

    public static AttendanceDay create(Date day, Person p)
    {
        AttendanceDay result = new AttendanceDay();
        result.person = p;
        result.day = day;
        result.save();

        return result;
    }

    public static AttendanceDay findOrCreate(Date day, Person p) {
        AttendanceDay attendance_day = AttendanceDay.find.where()
            .eq("person", p)
            .eq("day", day)
            .findUnique();

        if (attendance_day != null) return attendance_day;
        return create(day, p); 
    }

    public static Time parseTime(String time_string) {
        if (time_string == null || time_string.equals("")) {
            return null;
        }

		String[] formats = {"h:mm a", "h:mma", "h:mm"};

		for (String format : formats) {
			try {
				Date d = new SimpleDateFormat(format).parse(time_string);
				return new Time(d.getTime());
			} catch (ParseException e) {
			}
		}

		return null;
    }

    public void edit(String code, String start_time, String end_time) throws Exception {
        if (code.equals("")) {
            this.code = null;
        } else {
            this.code = code;
        }

        this.start_time = parseTime(start_time);
        this.end_time = parseTime(end_time);

        this.update();
    }

    @JsonIgnore
    public double getHours() {
        return (end_time.getTime() - start_time.getTime()) / (1000.0 * 60 * 60);
    }

    @JsonIgnore
    public boolean isPartial() {
        Organization org = OrgConfig.get().org;

        if (org.attendance_enable_partial_days) {
            Integer min_hours = org.attendance_day_min_hours;
            Time latest_start_time = org.attendance_day_latest_start_time;

            if (min_hours != null && getHours() < min_hours) {
                return true;
            }
            if (latest_start_time != null && start_time.getTime() > latest_start_time.getTime()) {
                return true;
            }
        }
        return false;
    }
}

@@ -1048,324 +1048,8 @@
  %7D%0A%0A
-    public static AttendanceDay findOrCreate(Date day, Person p) %7B%0A        AttendanceDay attendance_day = AttendanceDay.find.where()%0A            .eq(%22person%22, p)%0A            .eq(%22day%22, day)%0A            .findUnique();%0A%0A        if (attendance_day != null) return attendance_day;%0A        return create(day, p); %0A    %7D%0A%0A
     

Fix more attendance data creation stuff