package com.usai.redant.CommonEditor;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.NumberPicker;

import com.usai.redant.redantmobile.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MonthPickerActivity extends AppCompatActivity {

    public static class Builder {
        public Intent build(Context context, String date, int group, int child) {

            if (context == null) {
                return null;
            }
            Intent intent = new Intent(context,MonthPickerActivity.class);

            if (date != null) {
                intent.putExtra("date",date);
            }

            intent.putExtra("group",group);
            intent.putExtra("child",child);


            return intent;
        }
    }

    private NumberPicker yearPicker = null;
    private NumberPicker monthPicker = null;

    private int year;
    private int month;
    private int group;
    private int child;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_picker);


        yearPicker = (NumberPicker) findViewById(R.id.year_picker);
        yearPicker.setMaxValue(2050);
        yearPicker.setMinValue(2000);

        monthPicker = (NumberPicker) findViewById(R.id.month_picker);
        String[] monthArr = {"January","February","March","April","May","June","July","August","September","October","November","December"};
        monthPicker.setDisplayedValues(monthArr);
        monthPicker.setMinValue(0);
        monthPicker.setMaxValue(monthArr.length - 1);

        init();
    }

    private void init() {

        Intent intent = getIntent();


        group = intent.getIntExtra("group",0);
        child = intent.getIntExtra("child",0);
        String date_str = intent.getStringExtra("date");

        if (date_str != null) {
            String[] date_arr = date_str.split("/");
            year = Integer.valueOf(date_arr[1]);
            month = Integer.valueOf(date_arr[0]);
        } else {
            month = 1;
            year = 2017;
        }

        yearPicker.setValue(year);
        monthPicker.setValue(month - 1);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.set_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.set_item: {
                int year = yearPicker.getValue();
                int month = monthPicker.getValue() + 1;
                String date_str = month + "/" + year;
                Intent intent = new Intent();
                intent.putExtra("value",date_str);
                intent.putExtra("group",group);
                intent.putExtra("child",child);
                setResult(RESULT_OK,intent);
                finish();
            }
            break;
        }
        return true;
    }
}
