package com.philliphsu.bottomsheetpickers.view;

import android.content.Context;
import android.support.annotation.IdRes;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;

import com.philliphsu.bottomsheetpickers.R;

/**
 * View to pick an hour (00 - 23) from a 4 x 3 grid.
 */
public final class TwentyFourHourPickerView extends LinearLayout {

    private static final @IdRes int[] TEXT_SWITCHER_IDS = {
            R.id.switcher0,
            R.id.switcher1,
            R.id.switcher2,
            R.id.switcher3,
            R.id.switcher4,
            R.id.switcher5,
            R.id.switcher6,
            R.id.switcher7,
            R.id.switcher8,
            R.id.switcher9,
            R.id.switcher10,
            R.id.switcher11,
    };

    private static final int NUM_TEXT_SWITCHERS = TEXT_SWITCHER_IDS.length;

    private static final TextSwitcher[] TEXT_SWITCHERS = new TextSwitcher[NUM_TEXT_SWITCHERS];

    private static final String[] HOURS_TEXTS_00_11 = new String[12];
    private static final String[] HOURS_TEXTS_12_23 = new String[12];

    static {
        for (int i = 0; i < 12; i++) {
            HOURS_TEXTS_00_11[i] = String.format("%02d", i);
            HOURS_TEXTS_12_23[i] = String.format("%02d", i + 12);
        }
    }

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

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

    public TwentyFourHourPickerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.bsp_twentyfour_hour_picker_view, this);

        for (int i = 0; i < NUM_TEXT_SWITCHERS; i++) {
            TEXT_SWITCHERS[i] = (TextSwitcher) findViewById(TEXT_SWITCHER_IDS[i]);
            if (i < 12) {
                TEXT_SWITCHERS[i].setCurrentText(HOURS_TEXTS_00_11[i]);
                TEXT_SWITCHERS[i].setOnClickListener(mOnClickListener);
            }
        }
    }

//    @TargetApi(21)
//    public TwentyFourHourPickerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//    }

    // TODO: Proper implementation--not this anonymous inner class.
    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < 12; i++) {
                TEXT_SWITCHERS[i].setText(HOURS_TEXTS_12_23[i]);
            }
        }
    };
}

@@ -149,16 +149,45 @@
 uteSet;%0A
+import android.view.Gravity;%0A
 import a
@@ -226,22 +226,21 @@
 .widget.
-Linear
+Frame
 Layout;%0A
@@ -279,51 +279,184 @@
 er;%0A
-%0Aimport com.philliphsu.bottomsheetpickers.R
+import android.widget.TextView;%0Aimport android.widget.ViewSwitcher;%0A%0Aimport com.philliphsu.bottomsheetpickers.R;%0A%0Aimport static android.view.ViewGroup.LayoutParams.MATCH_PARENT
 ;%0A%0A/
@@ -571,14 +571,13 @@
 nds 
-Linear
+Frame
 Layo
@@ -2071,26 +2071,270 @@
     
-if (i %3C 12) %7B%0A    
+TEXT_SWITCHERS%5Bi%5D.setFactory(mFactory);%0A            // TODO: Load the animation once and set it here.%0A            TEXT_SWITCHERS%5Bi%5D.setInAnimation(context, android.R.anim.fade_in);%0A            TEXT_SWITCHERS%5Bi%5D.setOutAnimation(context, android.R.anim.fade_out);%0A
     
@@ -2393,28 +2393,24 @@
 _00_11%5Bi%5D);%0A
-    
             
@@ -2465,30 +2465,16 @@
 tener);%0A
-            %7D%0A
         
@@ -2903,34 +2903,50 @@
 (int i = 0; i %3C 
-12
+NUM_TEXT_SWITCHERS
 ; i++) %7B%0A       
@@ -3033,11 +3033,521 @@
 %7D%0A    %7D;
+%0A%0A    // TODO: Proper implementation--not this anonymous inner class.%0A    private ViewSwitcher.ViewFactory mFactory = new ViewSwitcher.ViewFactory() %7B%0A        @Override%0A        public View makeView() %7B%0A            TextView view = new TextView(getContext());%0A            view.setGravity(Gravity.CENTER);%0A            view.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));%0A            view.setTextAppearance(getContext(), R.style.BSP_PadButtonStyle_Numeric);%0A            return view;%0A        %7D%0A    %7D;
 %0A%7D%0A

Fix contents of TwentyFourHourPickerView not showing up.