package com.genlot.ninglottery.module.info.winnumber;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.genlot.nativesdk.model.GameHistoryNumber;
import com.genlot.nativesdk.sender.EventToken;
import com.genlot.nativesdk.sender.TCPSender;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseFragment;
import com.genlot.ninglottery.model.Constant;
import com.genlot.ninglottery.model.GameIDSet;
import com.genlot.ninglottery.module.info.InfoActivity;
import com.genlot.ninglottery.toolkit.LogTool;

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

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 中奖号码
 * @date 2019/10/30
 */
public class WinNumberFragment extends BaseFragment {

    private int mType;
    private static final String DATE_FORMAT = "%d%02d%02d";
    private int mYear, mMonth, mDay;
    private List<GameHistoryNumber> mList;
    private WinNumberAdapter mAdapter;

    public static WinNumberFragment getInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(Constant.KEY_TYPE, type);
        WinNumberFragment fragment = new WinNumberFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @OnClick({R.id.tv_query_date, R.id.btn_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_query_date:
                new DatePickerDialog(activity, onPickDateListener, mYear, mMonth, mDay).show();
                break;
            case R.id.btn_confirm:
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(System.currentTimeMillis());
                if (mYear > cal.get(Calendar.YEAR) || mMonth > cal.get(Calendar.MONTH)
                        || mDay > cal.get(Calendar.DAY_OF_MONTH)) {
                    tvDate.setError("日期选择错误");
                }
                break;
        }
    }

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

    @Override
    public void onCreateViewFinished(Bundle state) {
        if(getArguments() != null){
            mType = getArguments().getInt(Constant.KEY_TYPE);
        }
        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);
        mList = new ArrayList<>();
        mAdapter = new WinNumberAdapter(R.layout.item_win_number, mList);
        ryNumbers.setAdapter(mAdapter);
        ryNumbers.setLayoutManager(new LinearLayoutManager(activity));
        if (mType == InfoActivity.TYPE_NORMAL) {
            layoutDate.setVisibility(View.GONE);
            layoutContent.setVisibility(View.VISIBLE);
            tvTitle.setText(String.format(Locale.getDefault(),"%d年%02d月%02d日开奖号码",mYear, mMonth + 1, mDay));
            queryNumber();
        } else {
            tvDate.setText(String.format(Locale.getDefault(), DATE_FORMAT, mYear, mMonth + 1, mDay));
            layoutDate.setVisibility(View.VISIBLE);
            layoutContent.setVisibility(View.GONE);
        }
    }

    public boolean onBack() {
        if (mType == InfoActivity.TYPE_HISTORY) {
            if (layoutDate.getVisibility() == View.GONE) {
                layoutDate.setVisibility(View.VISIBLE);
                layoutContent.setVisibility(View.GONE);
                return true;
            }
        }
        return false;
    }

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

    private void queryNumber() {
        showLoading();
        TCPSender.t101459QueryHistoryNumber(EventToken.T_GET_HISTORY_NUMBER, 1, GameIDSet.GAME_ID_K3,
                new TCPSender.SenderCallback() {
            @Override
            public void onSenderSuccess(Bundle bundle) {
                dismissLoading();
                ArrayList<GameHistoryNumber> historyNumbers  = bundle.getParcelableArrayList(EventToken.EVENT_KEY_RESULT_OBJECT);
                LogTool.logD(historyNumbers.toString());
                activity.runOnUiThread(() -> mAdapter.replaceData(historyNumbers));
            }

            @Override
            public void onSenderFail(int code, String message) {
                dismissLoading();
                ToastUtils.showShort(message);
                LogTool.logE(message);
            }
        });
    }

    @BindView(R.id.tv_query_date)
    TextView tvDate;
    @BindView(R.id.layout_date_select)
    ViewGroup layoutDate;
    @BindView(R.id.layout_content)
    ViewGroup layoutContent;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_win_number)
    RecyclerView ryNumbers;

    class WinNumberAdapter extends BaseQuickAdapter<GameHistoryNumber, BaseViewHolder>{

        WinNumberAdapter(int layoutResId, @Nullable List<GameHistoryNumber> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, GameHistoryNumber item) {
            helper.setText(R.id.tv_issue, String.valueOf(item.mGameIssue));
            helper.setText(R.id.tv_number1, String.valueOf(item.mWinNumber.charAt(0)));
            helper.setText(R.id.tv_number2, String.valueOf(item.mWinNumber.charAt(1)));
            helper.setText(R.id.tv_number3, String.valueOf(item.mWinNumber.charAt(2)));
        }
    }

}
