package com.ogsdroid;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;

import org.json.JSONException;
import org.json.JSONObject;

class Clock {
    private final static String TAG = "Clock";

    public int thinkingTime;
    public int periods;
    public int periodTime;
    public String system; // fischer, byo-yomi, etc.

    private Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

    Clock() {
        this("simple");
    }

    Clock(String system) {
        this.system = system;

    }

    public void tick() {
        if (thinkingTime > 0) {
            thinkingTime--;
            return;
        }

        if (periods > 0) {
            periods--;
            thinkingTime = periodTime;
        }
    }

    public void setTime(int thinkingTime, int periods, int periodTime) {
        this.thinkingTime = thinkingTime;
        this.periods = periods;
        this.periodTime = periodTime;
    }

    public void set(JSONObject clock) {
        try {
            setTime(clock.getInt("thinking_time"),
                    clock.getInt("periods"),
                    clock.getInt("periodTime"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void draw(Canvas canvas, boolean black, String username, float sx, float sy, float w, float h, boolean mymove) {
        if (mymove) {
            if (urgent()) {
                p.setARGB(255, 255, 0, 0);
                p.setStrokeWidth(1);
                canvas.drawRect(sx, sy, sx + w, sy + h, p);
            } else {
                p.setARGB(255, 0, 128, 0);
                p.setStrokeWidth(1);
                canvas.drawRect(sx, sy, sx + w, sy + h, p);
            }
        }
        if (black) {
            p.setARGB(255, 0, 0, 0);
            p.setStrokeWidth(1);
        } else {
            p.setARGB(255, 255, 255, 255);
            p.setStrokeWidth(1);
        }
        canvas.drawRect(sx + 5, sy + 5, sx + w - 5, sy + h - 5, p);
        if (black) {
            p.setARGB(255, 255, 255, 255);
            p.setStrokeWidth(1);
        } else {
            p.setARGB(255, 0, 0, 0);
            p.setStrokeWidth(1);
        }
        p.setTextAlign(Paint.Align.CENTER);
        p.setTextSize(h / 8);
        p.setTypeface(Typeface.MONOSPACE);
        canvas.drawText(username, sx + w / 2, sy + h / 3, p);
        canvas.drawText(toString(), sx + w / 2, sy + h * 2 / 3, p);
    }

    protected String formatTime(int seconds) {
        int days = seconds / (24 * 60 * 60);
        seconds %= (24 * 60 * 60);
        int hours = seconds / (60 * 60);
        seconds %= (60 * 60);
        int minutes = seconds / 60;
        seconds %= 60;

        StringBuilder s = new StringBuilder();

        if (days > 0) {
            s.append(String.format("%dd %d:%02d:%02d", days, hours, minutes, seconds));
        } else if (hours > 0) {
            s.append(String.format("%d:%02d:%02d", hours, minutes, seconds));
        } else if (minutes > 0) {
            s.append(String.format("%d:%02d", minutes, seconds));
        } else {
            s.append(String.format("%d", seconds));
        }
        return s.toString();
    }

    public String toString() {
        if (thinkingTime <= 0 && periods == 0)
            return "";
        if (periods > 0) {
            return String.format("%s + %dx%s", formatTime(thinkingTime), periods, formatTime(periodTime));
        } else {
            return formatTime(thinkingTime);
        }
    }

    public boolean urgent() {
        return (thinkingTime < 10);
    }
}

@@ -247,19 +247,20 @@
  public 
-int
+long
  thinkin
@@ -273,27 +273,28 @@
 %0A    public 
-int
+long
  periods;%0A  
@@ -302,19 +302,20 @@
  public 
-int
+long
  periodT
@@ -786,19 +786,20 @@
 setTime(
-int
+long
  thinkin
@@ -805,19 +805,20 @@
 ngTime, 
-int
+long
  periods
@@ -819,19 +819,20 @@
 eriods, 
-int
+long
  periodT
@@ -2483,19 +2483,20 @@
 matTime(
-int
+long
  seconds
@@ -2507,19 +2507,20 @@
         
-int
+long
  days = 
@@ -2588,19 +2588,20 @@
         
-int
+long
  hours =
@@ -2660,19 +2660,20 @@
         
-int
+long
  minutes
@@ -2828,162 +2828,265 @@
 %22%25dd
- %25d:%2502d:%2502d%22, days, hours, minutes, seconds));%0A        %7D else if (hours %3E 0) %7B%0A            s.append(String.format(%22%25d:%2502d:%2502d%22, hours, minutes, second
+%22, days));%0A            if (hours %3E 0)%0A                s.append(String.format(%22%25dh%22, hours));%0A        %7D else if (hours %3E 0) %7B%0A            s.append(String.format(%22%25dh%22, hours));%0A            if (minutes %3E 0)%0A                s.append(String.format(%22%25dm%22, minute
 s));
@@ -3162,13 +3162,14 @@
 (%22%25d
-:
+m
 %2502d
+s
 %22, m
@@ -3242,16 +3242,17 @@
 rmat(%22%25d
+s
 %22, secon

improve the display of the clock