package com.genlot.ninglottery.game;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.genlot.nativesdk.model.GameRuntimeAttr;
import com.genlot.nativesdk.model.GameTicket;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseActivity;
import com.genlot.ninglottery.event.NewIssueEvent;
import com.genlot.ninglottery.game.bet.BetManager;
import com.genlot.ninglottery.game.common.CommonFragment;
import com.genlot.ninglottery.game.d3.D3Fragment;
import com.genlot.ninglottery.game.k3.K3Fragment;
import com.genlot.ninglottery.game.qlc.QLCFragment;
import com.genlot.ninglottery.game.ssq.SSQFragment;
import com.genlot.ninglottery.model.Constant;
import com.genlot.ninglottery.model.GameIDSet;
import com.genlot.ninglottery.module.scan.ScanActivity;
import com.genlot.ninglottery.toolkit.LogTool;
import com.genlot.ninglottery.widget.BottomView;
import com.genlot.ninglottery.widget.TitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

/**
 * @author ZhuYongKang
 * @description 游戏界面
 * @date 2019/8/19
 */
public class GameActivity extends BaseActivity {

    private CommonFragment mFragment;
    private int mGameId;
    private Intent intent;

    @OnCheckedChanged({R.id.radio_ssq, R.id.radio_3d, R.id.radio_7lc, R.id.radio_k3})
    public void onGameChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.radio_ssq:
                    LogTool.logD("changeGame 双色球");
                    mGameId = GameIDSet.GAME_ID_SSQ;
                    replaceFragment(new SSQFragment());
                    break;
                case R.id.radio_3d:
                    LogTool.logD("changeGame 福彩3D");
                    mGameId = GameIDSet.GAME_ID_3D;
                    replaceFragment(new D3Fragment());
                    break;
                case R.id.radio_7lc:
                    LogTool.logD("changeGame 7乐彩");
                    mGameId = GameIDSet.GAME_ID_SLC;
                    replaceFragment(new QLCFragment());
                    break;
                case R.id.radio_k3:
                    LogTool.logD("changeGame 快3");
                    mGameId = GameIDSet.GAME_ID_K3;
                    replaceFragment(new K3Fragment());
                    break;
                default:
                    break;
            }
            showGameIssue(mGameId);
            BetManager.getInstance().setGameId(mGameId);
        }
    }

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

    @Override
    public void initViews(Bundle savedInstanceState) {
        radioSSQ.setChecked(true);
        if (!GameManager.isTrainMode) {
            intent = new Intent(this, GameService.class);
            startService(intent);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!GameManager.isTrainMode) {
            stopService(intent);
        }
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNewIssue(NewIssueEvent event) {
        int gameId = event.getGameID();
        LogTool.logD("onNewIssue = " + gameId);
        if (mGameId == gameId) {
            showGameIssue(gameId);
        }
    }

    /**
     * 复制成功
     * @param ticket GameTicket
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCopySuccess(GameTicket ticket) {
        changeGame(ticket.mGameID);
        mFragment.onCopySuccess(ticket);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessage(String tips) {
        showTips(tips);
    }

    private void showGameIssue(int mGameId) {
        if (GameManager.isTrainMode) {
            mTitleView.showTrainMode();
            return;
        }
        GameRuntimeAttr attr = GameManager.getGameRuntime(mGameId);
        if (attr != null) {
            if (mGameId == GameIDSet.GAME_ID_K3) {
                mTitleView.setGameInfo(String.valueOf(attr.mGameIssue), attr.mDrawTime);
            } else {
                mTitleView.setGameInfo(String.valueOf(attr.mGameIssue), -1);
            }
        }
    }

    public void changeGame(int mGameId) {
        switch (mGameId) {
            case GameIDSet.GAME_ID_SSQ:
                radioSSQ.setChecked(true);
                break;
            case GameIDSet.GAME_ID_3D:
                radio3D.setChecked(true);
                break;
            case GameIDSet.GAME_ID_SLC:
                radio7lc.setChecked(true);
                break;
            case GameIDSet.GAME_ID_K3:
                radioK3.setChecked(true);
                break;
            default:
                break;
        }
    }

    private void replaceFragment(CommonFragment targetFragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content, targetFragment)
                .commit();
        mFragment = targetFragment;
    }

    public void showTips(String message) {
        mBottomView.showTips(message);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            showTips("");
        }
        return super.dispatchTouchEvent(ev);
    }

//    private void addScanFragment(BaseFragment scanFragment) {
//        if (mScanFragment != null && mScanFragment.isResumed() && mScanFragment == scanFragment) {
//            return;
//        }
//        mScanFragment = scanFragment;
//        getSupportFragmentManager().beginTransaction()
//                .replace(R.id.content, mScanFragment)
//                .commit();
//        radioGroup.clearCheck();
//    }

//    public void backToGame() {
//        mScanFragment = null;
//        changeGame(mGameId);
//    }

    @OnClick(R.id.bt_cash)
    public void showCashFragment() {
        Intent intent = new Intent(this, ScanActivity.class);
        intent.putExtra(Constant.KEY_TYPE, Constant.TYPE_CASH);
        startActivity(intent);
    }

    @OnClick(R.id.bt_copy)
    public void showCopyDialog() {
        Intent intent = new Intent(this, ScanActivity.class);
        intent.putExtra(Constant.KEY_TYPE, Constant.TYPE_COPY);
        startActivity(intent);
    }

    @OnClick(R.id.bt_cancel)
    public void showCancelDialog() {
        Intent intent = new Intent(this, ScanActivity.class);
        intent.putExtra(Constant.KEY_TYPE, Constant.TYPE_CANCEL);
        startActivity(intent);
    }

    @BindView(R.id.title)
    TitleView mTitleView;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.radio_ssq)
    RadioButton radioSSQ;
    @BindView(R.id.radio_3d)
    RadioButton radio3D;
    @BindView(R.id.radio_7lc)
    RadioButton radio7lc;
    @BindView(R.id.radio_k3)
    RadioButton radioK3;
    @BindView(R.id.bottom_view)
    BottomView mBottomView;

}
