package com.genlot.ninglottery.module.report.fragments;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseFragment;
import com.genlot.ninglottery.model.GameIDSet;
import com.genlot.ninglottery.module.report.ReportActivity;
import com.genlot.ninglottery.widget.CustomSpinnerDropDown.SpinnerPopWindow;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 时段报表选择
 * @date 2019/8/20
 */
public class ReportTimeFragment extends BaseFragment {

    private String mStartDate;
    private String mEndDate;
    private int mYear = 0;
    private int mMonth = 0;
    private int mDay = 0;
    private static final String DATE_FORMAT = "%d年%02d月%02d日";
    private SpinnerPopWindow mGameTypeSpinner;
    private int mGameId;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_report_time;
    }

    @Override
    public void onCreateViewFinished(Bundle state) {
        mGameId = -1;
        initDate();
        initSpinner();
    }

    private void initDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        mYear = cal.get(Calendar.YEAR);
        mMonth = cal.get(Calendar.MONTH);
        mDay = cal.get(Calendar.DAY_OF_MONTH);
        mEdStartRange.setText(String.format(Locale.getDefault(), DATE_FORMAT, mYear, mMonth + 1, mDay));
        mEdEndRange.setText(String.format(Locale.getDefault(), DATE_FORMAT, mYear, mMonth + 1, mDay));
    }

    private void initSpinner() {
        mGameTypeSpinner = new SpinnerPopWindow(getActivity(), mTvGameType);
        List<String> gameNameList = new ArrayList<>(Arrays.asList("全部游戏","双色球", "3D", "七乐彩", "快三"));
        mGameTypeSpinner.refreshData(gameNameList);
        mTvGameType.setText(gameNameList.get(0));
        mGameTypeSpinner.setItemListener((view, pos) -> {
            mTvGameType.setText(gameNameList.get(pos));
            switch (pos) {
                case 0:
                    mGameId = -1;
                    break;
                case 1:
                    mGameId = GameIDSet.GAME_ID_SSQ;
                    break;
                case 2:
                    mGameId = GameIDSet.GAME_ID_3D;
                    break;
                case 3:
                    mGameId = GameIDSet.GAME_ID_SLC;
                    break;
                case 4:
                    mGameId = GameIDSet.GAME_ID_K3;
                    break;

            }
        });
    }

    @OnClick({R.id.tv_game_type, R.id.btn_confirm, R.id.tv_query_start_range, R.id.tv_query_end_range})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_game_type:
                mGameTypeSpinner.setWidth(mTvGameType.getWidth());
                mGameTypeSpinner.setHeight(240);
                mGameTypeSpinner.showAsDropDown(mTvGameType);
                break;
            case R.id.tv_query_start_range:
                new DatePickerDialog(activity, onStartDateListener, mYear, mMonth, mDay).show();
                break;
            case R.id.tv_query_end_range:
                new DatePickerDialog(activity, onEndDateListener, mYear, mMonth, mDay).show();
                break;
            case R.id.btn_confirm:
                mStartDate = mEdStartRange.getText().toString().trim();
                mEndDate = mEdEndRange.getText().toString().trim();
                if (TextUtils.isEmpty(mStartDate)) {
                    ToastUtils.showShort("请选择开始日期");
                    return;
                }
                if (TextUtils.isEmpty(mEndDate)) {
                    ToastUtils.showShort("请输入结束日期");
                    return;
                }
                ((ReportActivity) activity).goReportDetailFragment(mGameId, mStartDate, mEndDate);
                break;
            default:
                break;
        }
    }

    private DatePickerDialog.OnDateSetListener onStartDateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            mEdStartRange.setText(String.format(Locale.getDefault(), DATE_FORMAT, mYear, mMonth + 1, mDay));
        }
    };

    private DatePickerDialog.OnDateSetListener onEndDateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            mEdEndRange.setText(String.format(Locale.getDefault(), DATE_FORMAT, mYear, mMonth + 1, mDay));
        }
    };

    @BindView(R.id.tv_game_type)
    TextView mTvGameType;
    @BindView(R.id.tv_query_start_range)
    TextView mEdStartRange;
    @BindView(R.id.tv_query_end_range)
    TextView mEdEndRange;

}
