package info.nightscout.androidaps.interaction.actions;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.wearable.view.DotsPageIndicator;
import android.support.wearable.view.GridPagerAdapter;
import android.support.wearable.view.GridViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.text.DecimalFormat;

import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.ListenerService;
import info.nightscout.androidaps.interaction.utils.PlusMinusEditText;
import info.nightscout.androidaps.interaction.utils.SafeParse;

/**
 * Created by adrian on 09/02/17.
 */


public class CPPActivity extends ViewSelectorActivity {

    PlusMinusEditText editPercentage;
    PlusMinusEditText editTimeshift;

    int percentage = -1;
    int timeshift = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        percentage = extras.getInt("percentage", -1);
        timeshift = extras.getInt("timeshift", -1);

        if (percentage ==-1 || timeshift ==-1){
            finish(); return;
        }

        setContentView(R.layout.grid_layout);
        final Resources res = getResources();
        final GridViewPager pager = (GridViewPager) findViewById(R.id.pager);

        pager.setAdapter(new MyGridViewPagerAdapter());
        DotsPageIndicator dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator);
        dotsPageIndicator.setPager(pager);
    }


    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }


    private class MyGridViewPagerAdapter extends GridPagerAdapter {
        @Override
        public int getColumnCount(int arg0) {
            return 3;
        }

        @Override
        public int getRowCount() {
            return 1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int row, int col) {

            if(col == 0){
                final View view = getInflatedPlusMinusView(container);
                double def = timeshift;
                if (editTimeshift != null){
                    def = SafeParse.stringToDouble(editTimeshift.editText.getText().toString());
                }
                editTimeshift = new PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, def, 0d, 23d, 1d, new DecimalFormat("0"), true, true);
                setLabelToPlusMinusView(view, "timeshift");
                container.addView(view);
                return view;
            } else if(col == 1){
                final View view = getInflatedPlusMinusView(container);
                double def = percentage;
                if (editPercentage != null){
                    def = SafeParse.stringToDouble(editPercentage.editText.getText().toString());
                }
                editPercentage = new PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, def, 50d, 150d, 1d, new DecimalFormat("0"), false);
                setLabelToPlusMinusView(view, "percentage");
                container.addView(view);
                return view;
            } else {

                final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_send_item, container, false);
                final ImageView confirmbutton = (ImageView) view.findViewById(R.id.confirmbutton);
                confirmbutton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        //check if it can happen that the fagment is never created that hold data?
                        // (you have to swipe past them anyways - but still)

                        String actionstring = "cppset " +SafeParse.stringToInt(editTimeshift.editText.getText().toString())
                                + " " + SafeParse.stringToInt(editPercentage.editText.getText().toString());
                        ListenerService.initiateAction(CPPActivity.this, actionstring);
                        finish();
                    }
                });
                container.addView(view);
                return view;
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int row, int col, Object view) {
            // Handle this to get the data before the view is destroyed?
            // Object should still be kept by this, just setup for reinit?
            container.removeView((View)view);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }


    }
}
@@ -889,17 +889,18 @@
 hift = -
-1
+25
 ;%0A%0A    @
@@ -1210,17 +1210,18 @@
 hift ==-
-1
+25
 )%7B%0A     
@@ -1248,32 +1248,76 @@
 urn;%0A        %7D%0A%0A
+        if(timeshift %3C 0) timeshift += 24;%0A%0A
         setConte
@@ -4850,8 +4850,9 @@
 %0A    %7D%0A%7D
+%0A

timeshift can be negative on watch