package com.philliphsu.bottomsheetpickers.view.numberpad;

import android.content.Context;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.widget.TextView;

import com.philliphsu.bottomsheetpickers.view.DateTimeFormatUtils;
import com.philliphsu.bottomsheetpickers.view.GridPickerView;

import java.text.DateFormatSymbols;

/**
 * Created by Phillip Hsu on 4/4/2017.
 */

class NumberPadTimePickerView extends GridPickerView {
    /**
     * Indices map to buttons that represent those numbers.
     * E.g. index 0 -> zero button (located at position 10 in the grid).
     */
    private final TextView[] mNumberButtons = new TextView[10];

    private final TextView[] mAltButtons = new TextView[2];

    private boolean mIs24HourMode;

    public NumberPadTimePickerView(Context context) {
        this(context, null);
    }

    public NumberPadTimePickerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberPadTimePickerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        for (int i = 0; i < 9; i++) {
            setTextForPosition(i, String.format("%d", i + 1));
        }
        setTextForPosition(10, String.format("%d", 0));

        final boolean is24HourMode = DateFormat.is24HourFormat(context);
        setAltKeysTexts(is24HourMode);
        mIs24HourMode = is24HourMode;

        // Store our own references to the grid's buttons by
        // mapping an index to the button that represents that
        // number.
        mNumberButtons[0] = getButton(10);
        for (int i = 0; i < mNumberButtons.length - 1; i++) {
            mNumberButtons[i + 1] = getButton(i);
        }

        mAltButtons[0] = getButton(9);
        mAltButtons[1] = getButton(11);
    }

    void setNumberKeysEnabled(int lowerLimitInclusive, int upperLimitExclusive) {
        if (lowerLimitInclusive < 0 || upperLimitExclusive > mNumberButtons.length)
            throw new IndexOutOfBoundsException("Upper limit out of range");

        for (int i = 0; i < mNumberButtons.length; i++) {
            mNumberButtons[i].setEnabled(i >= lowerLimitInclusive && i < upperLimitExclusive);
        }
    }

    void setOnNumberKeyClickListener(OnClickListener l) {
        for (TextView tv : mNumberButtons) {
            tv.setOnClickListener(l);
        }
    }

    void setOnAltKeyClickListener(OnClickListener l) {
        mAltButtons[0].setOnClickListener(l);
        mAltButtons[1].setOnClickListener(l);
    }

    void setIs24HourMode(boolean is24HourMode) {
        if (mIs24HourMode != is24HourMode) {
            setAltKeysTexts(is24HourMode);
            mIs24HourMode = is24HourMode;
        }
    }

    void setLeftAltKeyEnabled(boolean enabled) {
        mAltButtons[0].setEnabled(enabled);
    }

    void setRightAltKeyEnabled(boolean enabled) {
        mAltButtons[1].setEnabled(enabled);
    }

    private boolean is24HourFormat() {
        return mIs24HourMode;
    }

    private void setAltKeysTexts(boolean is24HourMode) {
        final String altText1, altText2;
        if (is24HourMode) {
            final String timeSeparator = DateTimeFormatUtils.getTimeSeparator(getContext(), true);
            altText1 = timeSeparator + String.format("%02d", 0);
            altText2 = timeSeparator + String.format("%02d", 30);
        } else  {
            String[] amPm = new DateFormatSymbols().getAmPmStrings();
            // TODO: Get localized. Or get the same am/pm strings as the framework.
            altText1 = amPm[0].length() > 2 ? "AM" : amPm[0];
            altText2 = amPm[1].length() > 2 ? "PM" : amPm[1];
        }
        // TODO: Apply a smaller text size.
        setTextForPosition(9, altText1);
        // TODO: Apply a smaller text size.
        setTextForPosition(11, altText2);
    }
}
@@ -88,47 +88,8 @@
 xt;%0A
-import android.text.format.DateFormat;%0A
 impo
@@ -118,16 +118,16 @@
 uteSet;%0A
+
 import a
@@ -1069,327 +1069,8 @@
 e);%0A
-        for (int i = 0; i %3C 9; i++) %7B%0A            setTextForPosition(i, String.format(%22%25d%22, i + 1));%0A        %7D%0A        setTextForPosition(10, String.format(%22%25d%22, 0));%0A%0A        final boolean is24HourMode = DateFormat.is24HourFormat(context);%0A        setAltKeysTexts(is24HourMode);%0A        mIs24HourMode = is24HourMode;%0A%0A
     
@@ -1247,24 +1247,83 @@
 Button(10);%0A
+        mNumberButtons%5B0%5D.setText(String.format(%22%25d%22, 0));%0A
         for 
@@ -1364,32 +1364,138 @@
 gth - 1; i++) %7B%0A
+            final TextView button = getButton(i);%0A            button.setText(String.format(%22%25d%22, i + 1));%0A
             mNum
@@ -1514,28 +1514,22 @@
  + 1%5D = 
-getB
+b
 utton
-(i)
 ;%0A      
@@ -2337,32 +2337,48 @@
 ener(l);%0A    %7D%0A%0A
+    @Deprecated%0A
     void setIs24
@@ -2826,32 +2826,48 @@
 ourMode;%0A    %7D%0A%0A
+    @Deprecated%0A
     private void
@@ -3633,32 +3633,32 @@
 ller text size.%0A
-
         setTextF
@@ -3688,10 +3688,197 @@
 );%0A    %7D
+%0A%0A    void setLeftAltKeyText(CharSequence text) %7B%0A        mAltButtons%5B0%5D.setText(text);%0A    %7D%0A%0A    void setRightAltKeyText(CharSequence text) %7B%0A        mAltButtons%5B1%5D.setText(text);%0A    %7D
 %0A%7D

Add methods setLeftAltKeyText() and setRightAltKeyText().