package com.philliphsu.bottomsheetpickers.view.numberpad;

import android.support.annotation.Nullable;

import java.util.Arrays;

/**
 * Model that encapsulates data pertaining to the inputted time
 * in a number pad time picker.
 */
final class DigitwiseTimeModel {
    private static final int UNMODIFIED = -1;

    static final int MAX_DIGITS = 4;

    private final int[] mInput = new int[MAX_DIGITS];

    private final @Nullable OnInputChangeListener mListener;

    private int mCount;

    /**
     * Informs clients of new digit insertion and deletion events.
     */
    interface OnInputChangeListener {
        /**
         * @param digit The stored digit.
         */
        void onDigitStored(int digit);

        /**
         * @param digit The removed digit.
         */
        void onDigitRemoved(int digit);

        void onDigitsCleared();
    }

    // TODO: Delete this!
    @Deprecated
    DigitwiseTimeModel() {
        this(null);
    }

    DigitwiseTimeModel(OnInputChangeListener listener) {
        mListener = listener;

        // TOneverDO: Call clearDigits() to do this, otherwise we'll
        // end up calling back to the listener with an unintended
        // onDigitsCleared() event.
        Arrays.fill(mInput, UNMODIFIED);
        mCount = 0;
    }

    void storeDigit(int digit) {
        if (mCount < MAX_DIGITS) {
            mInput[mCount] = digit;
            mCount++;
            if (mListener != null) {
                mListener.onDigitStored(digit);
            }
        }
    }

    int getDigit(int at) {
        return mInput[at];
    }

    /**
     * @return a defensive copy of the internal array of inputted digits
     */
    int[] getDigits() {
        int[] digits = new int[mInput.length];
        System.arraycopy(mInput, 0, digits, 0, mInput.length);
        return digits;
    }

    void removeDigit() {
        if (mCount > 0) {
            mCount--; // move the cursor back
            int digit = mInput[mCount];
            mInput[mCount] = UNMODIFIED;
            if (mListener != null) {
                mListener.onDigitRemoved(digit);
            }
        }
    }

    boolean clearDigits() {
        Arrays.fill(mInput, UNMODIFIED);
        mCount = 0;
        if (mListener != null) {
            mListener.onDigitsCleared();
        }
        return true;
    }

    int count() {
        return mCount;
    }

    /**
     * @return the integer represented by the inputted digits
     */
    int getInput() {
        if (mCount <= 0) return UNMODIFIED;

        int result = 0;
        for (int i = 0; i < mCount; i++) {
            result = result * 10 + mInput[i];
        }
        return result;
    }

    /**
     * Inserts as many of the digits in the given sequence
     * into the input as possible.
     */
    void storeDigits(int... digits) {
        if (digits == null)
            return;
        for (int d : digits) {
            if (d < 0 || d > 9)
                throw new IllegalArgumentException("Not a digit " + d);
            if (mCount == MAX_DIGITS)
                break;
            storeDigit(d);
        }
    }
}

@@ -190,19 +190,16 @@
 ted time
-%0A *
  in a nu
@@ -220,16 +220,16 @@
 picker.%0A
+
  */%0Afina
@@ -367,21 +367,22 @@
  int%5B%5D m
-Input
+Digits
  = new i
@@ -1227,37 +1227,38 @@
    Arrays.fill(m
-Input
+Digits
 , UNMODIFIED);%0A 
@@ -1356,37 +1356,38 @@
  %7B%0A            m
-Input
+Digits
 %5BmCount%5D = digit
@@ -1569,21 +1569,22 @@
 return m
-Input
+Digits
 %5Bat%5D;%0A  
@@ -1733,21 +1733,22 @@
 ew int%5Bm
-Input
+Digits
 .length%5D
@@ -1775,21 +1775,22 @@
 aycopy(m
-Input
+Digits
 , 0, dig
@@ -1798,21 +1798,22 @@
 ts, 0, m
-Input
+Digits
 .length)
@@ -1966,21 +1966,22 @@
 igit = m
-Input
+Digits
 %5BmCount%5D
@@ -1995,21 +1995,22 @@
        m
-Input
+Digits
 %5BmCount%5D
@@ -2190,21 +2190,22 @@
 s.fill(m
-Input
+Digits
 , UNMODI
@@ -2638,21 +2638,22 @@
 * 10 + m
-Input
+Digits
 %5Bi%5D;%0A   

Rename member 'mInput' to 'mDigits' in DigitwiseTimeModel.