package com.genlot.ninglottery.module.info;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseDialogActivity;
import com.genlot.ninglottery.model.GameIDSet;
import com.genlot.ninglottery.module.info.address.AddressFragment;
import com.genlot.ninglottery.module.info.bulletin.BulletinFragment;
import com.genlot.ninglottery.module.info.notice.NoticeFragment;
import com.genlot.ninglottery.module.info.wininfo.WinInfoFragment;
import com.genlot.ninglottery.module.info.winnumber.WinNumberFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 信息查询
 * @date 2019/9/12
 */
public class InfoActivity extends BaseDialogActivity {

    private List<RadioGroup> mRadioMenus;
    private RadioGroup mCurrentGroup;
    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_HISTORY = 1;
    private int mSelectGame;

    @OnCheckedChanged({R.id.radio_notice, R.id.radio_bulletin, R.id.radio_win_info, R.id.radio_address,
            R.id.radio_win_number, R.id.radio_read_notice, R.id.radio_history_notice, R.id.radio_read_bulletin,
            R.id.radio_history_bulletin, R.id.radio_read_win_info, R.id.radio_history_win_info, R.id.radio_read_address,
            R.id.radio_download_address, R.id.radio_today_number, R.id.radio_history_number})
    public void checkChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.radio_notice:
                    showRadioGroup(rgNotice);
                    hideFragment();
                    break;
                case R.id.radio_bulletin:
                    showRadioGroup(rgBulletinGames);
                    hideFragment();
                    break;
                case R.id.radio_win_info:
                    showRadioGroup(rgWinInfoGames);
                    hideFragment();
                    break;
                case R.id.radio_address:
                    showRadioGroup(rgAddress);
                    hideFragment();
                    break;
                case R.id.radio_win_number:
                    showRadioGroup(rgWinNumber);
                    hideFragment();
                    break;
                case R.id.radio_read_notice:
                    replaceFragment(NoticeFragment.getInstance(TYPE_NORMAL));
                    break;
                case R.id.radio_history_notice:
                    replaceFragment(NoticeFragment.getInstance(TYPE_HISTORY));
                    break;
                case R.id.radio_read_bulletin:
                    replaceFragment(BulletinFragment.getInstance(TYPE_NORMAL, mSelectGame));
                    break;
                case R.id.radio_history_bulletin:
                    replaceFragment(BulletinFragment.getInstance(TYPE_HISTORY, mSelectGame));
                    break;
                case R.id.radio_read_win_info:
                    replaceFragment(WinInfoFragment.getInstance(TYPE_NORMAL, mSelectGame));
                    break;
                case R.id.radio_history_win_info:
                    replaceFragment(WinInfoFragment.getInstance(TYPE_HISTORY, mSelectGame));
                    break;
                case R.id.radio_read_address:
                    replaceFragment(AddressFragment.getInstance(TYPE_NORMAL));
                    break;
                case R.id.radio_download_address:
                    replaceFragment(AddressFragment.getInstance(TYPE_HISTORY));
                    break;
                case R.id.radio_today_number:
                    replaceFragment(WinNumberFragment.getInstance(TYPE_NORMAL));
                    break;
                case R.id.radio_history_number:
                    replaceFragment(WinNumberFragment.getInstance(TYPE_HISTORY));
                    break;
            }
        }
    }

    @OnClick({R.id.bulletin_game_ssq, R.id.bulletin_game_3d, R.id.bulletin_game_7lc,
            R.id.win_info_game_ssq, R.id.win_info_game_3d, R.id.win_info_game_7lc, R.id.win_info_game_k3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bulletin_game_ssq:
                showRadioGroup(rgBulletin);
                mSelectGame = GameIDSet.GAME_ID_SSQ;
                break;
            case R.id.win_info_game_ssq:
                showRadioGroup(rgWinInfo);
                mSelectGame = GameIDSet.GAME_ID_SSQ;
                break;
            case R.id.bulletin_game_3d:
                showRadioGroup(rgBulletin);
                mSelectGame = GameIDSet.GAME_ID_3D;
                break;
            case R.id.win_info_game_3d:
                showRadioGroup(rgWinInfo);
                mSelectGame = GameIDSet.GAME_ID_3D;
                break;
            case R.id.bulletin_game_7lc:
                showRadioGroup(rgBulletin);
                mSelectGame = GameIDSet.GAME_ID_SLC;
                break;
            case R.id.win_info_game_7lc:
                showRadioGroup(rgWinInfo);
                mSelectGame = GameIDSet.GAME_ID_SLC;
                break;
            case R.id.win_info_game_k3:
                showRadioGroup(rgWinInfo);
                mSelectGame = GameIDSet.GAME_ID_K3;
                break;
        }
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_info;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        mRadioMenus = new ArrayList<>();
        mRadioMenus.add(rgMain);
        mRadioMenus.add(rgNotice);
        mRadioMenus.add(rgBulletin);
        mRadioMenus.add(rgWinInfo);
        mRadioMenus.add(rgBulletinGames);
        mRadioMenus.add(rgWinInfoGames);
        mRadioMenus.add(rgAddress);
        mRadioMenus.add(rgWinNumber);
        mCurrentGroup = rgMain;
    }

    private void showRadioGroup(RadioGroup radioGroup) {
        for (RadioGroup group : mRadioMenus) {
            if (radioGroup == group) {
                radioGroup.setVisibility(View.VISIBLE);
            } else {
                group.setVisibility(View.GONE);
            }
        }
        radioGroup.clearCheck();
        mCurrentGroup = radioGroup;
        if (radioGroup == rgMain) {
            btClose.setVisibility(View.VISIBLE);
            btBack.setVisibility(View.GONE);
        } else {
            btClose.setVisibility(View.GONE);
            btBack.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.icon_back)
    public void goBack() {
        if (mCurrentGroup == rgNotice) {
            if (mCurrentFragment instanceof NoticeFragment &&
                    ((NoticeFragment) mCurrentFragment).onBack()) {
                return;
            }
        }
        if (mCurrentGroup == rgWinNumber) {
            if (mCurrentFragment instanceof WinNumberFragment &&
                    ((WinNumberFragment) mCurrentFragment).onBack()) {
                return;
            }
        }
        if (mCurrentGroup == rgBulletin) {
            if (mCurrentFragment instanceof BulletinFragment &&
                    ((BulletinFragment) mCurrentFragment).onBack()) {
                return;
            }
            showRadioGroup(rgBulletinGames);
            hideFragment();
            return;
        }
        if (mCurrentGroup == rgWinInfo) {
            if (mCurrentFragment instanceof  WinInfoFragment &&
                    ((WinInfoFragment) mCurrentFragment).onBack()) {
               return;
            }
            showRadioGroup(rgWinInfoGames);
            hideFragment();
            return;
        }
        showRadioGroup(rgMain);
        hideFragment();
    }

    @BindView(R.id.rg_main)
    RadioGroup rgMain;
    @BindView(R.id.rg_notice)
    RadioGroup rgNotice;
    @BindView(R.id.rg_bulletin)
    RadioGroup rgBulletin;
    @BindView(R.id.rg_win_info)
    RadioGroup rgWinInfo;
    @BindView(R.id.rg_bulletin_games)
    RadioGroup rgBulletinGames;
    @BindView(R.id.rg_win_info_games)
    RadioGroup rgWinInfoGames;
    @BindView(R.id.rg_address)
    RadioGroup rgAddress;
    @BindView(R.id.rg_win_number)
    RadioGroup rgWinNumber;

    @BindView(R.id.icon_back)
    public Button btBack;
}
