package com.android.chmo.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.aigestudio.wheelpicker.widgets.WheelMonthPicker;
import com.aigestudio.wheelpicker.widgets.WheelYearPicker;
import com.android.chmo.R;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PickMonthDialog extends Dialog {
    @BindView(R.id.picker_year)
    WheelYearPicker  yearPicker;
    @BindView(R.id.picker_month)
    WheelMonthPicker monthPicker;

    private OnPickYearMonthListener onPickYearMonthListener;

    public OnPickYearMonthListener getOnPickYearMonthListener() {
        return onPickYearMonthListener;
    }

    public void setOnPickYearMonthListener(OnPickYearMonthListener onPickYearMonthListener) {
        this.onPickYearMonthListener = onPickYearMonthListener;
    }

    public PickMonthDialog(@NonNull Context context) {
        super(context, R.style.DialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_pick_month);
        ButterKnife.bind(this);
        Window window = getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        window.setAttributes(wlp);
        window.setWindowAnimations(R.style.DialogAnimation);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

        Calendar calendar = Calendar.getInstance();
        yearPicker.setYearStart(calendar.get(Calendar.YEAR) - 5);
        yearPicker.setYearEnd(calendar.get(Calendar.YEAR));
        yearPicker.setSelectedYear(calendar.get(Calendar.YEAR));
        monthPicker.setSelectedMonth(calendar.get(Calendar.MONTH + 1));
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                if (onPickYearMonthListener != null) {
                    onPickYearMonthListener.onPickYearMonth(yearPicker.getSelectedYear(), monthPicker.getSelectedItemPosition() + 1);
                }
                dismiss();
        }
    }

    public interface OnPickYearMonthListener {
        void onPickYearMonth(int year, int month);
    }
}
