package com.jyc.threegames.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.jyc.threegames.App;
import com.jyc.threegames.R;
import com.jyc.threegames.activity.base.BaseActivity;
import com.jyc.threegames.bean.result.ResGameInfo;
import com.jyc.threegames.controller.GameController;
import com.jyc.threegames.net.SimpleRequest;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

public class RingActivity extends BaseActivity {
    private static final String PARAM_GAME_INFO = "info";

    public static final int TYPE_GAME = 0;
    public static final int TYPE_SCALE = 1;

    @BindView(R.id.label)
    TextView mTVLabel;

    private ResGameInfo mGameInfo;

    private boolean mGoToGameOrScale = false;

    private int mCurrentVolume = 0;

    private Ringtone mRingTone;

    @Override
    protected void init(Bundle instance) {
        super.init(instance);

        App.CAN_PLAY_GAME = false;

        if (instance == null)
            mGameInfo = getIntent().getParcelableExtra(PARAM_GAME_INFO);
        else
            mGameInfo =  instance.getParcelable(PARAM_GAME_INFO);

        if (mGameInfo == null){
            finish();
            return;
        }

        mTVLabel.setText(getLabel());

        maxVolume();
        playRing();
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(PARAM_GAME_INFO, mGameInfo);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mGameInfo = savedInstanceState.getParcelable(PARAM_GAME_INFO);
    }

    @Override
    protected int getRootLayout() {
        return R.layout.activity_ring;
    }

    @Override
    protected void onDestroy() {
        if (!mGoToGameOrScale)
            App.CAN_PLAY_GAME = true;
        stopPlay();
        restoreVolume();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() { }

    @OnClick(R.id.now)
    public void clickNow(){
        mGoToGameOrScale = true;
        if (mGameInfo.playGameType == ResGameInfo.GAME_TYPE_GAME){
            GameActivity.LAUNCH_GAME(this, mGameInfo);
            finish();
        }
    }

    @OnClick(R.id.push_back)
    public void clickPushBack(){
        String[] items = new String[]{"5分鐘", "10分鐘", "15分鐘", "20分鐘", "25分鐘", "30分鐘"};
        new AlertDialog.Builder(this,0)
            .setTitle("請選擇延遲時間")
            .setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (mGameInfo.playGameType == ResGameInfo.GAME_TYPE_GAME){
                        dialogInterface.dismiss();
                        Dialog loading = new ProgressDialog(RingActivity.this);
                        loading.setTitle("正在延遲");
                        loading.show();
                        new SimpleRequest<>().request(RingActivity.this,
                            GameController.getInstance().delayGame(mGameInfo.playGameId, mGameInfo.gameConfigId, getDelayMin(i)), "延遲失敗!請檢查網絡連接狀態", loading, new SimpleRequest.Executor<Object>() {
                                @Override
                                public void execute(Object obj) {
                                    Toast.makeText(RingActivity.this, "延遲成功", Toast.LENGTH_LONG).show();
                                    finish();
                                }
                            });
                    }

                }
            }).create().show();
    }

    private int getDelayMin(int index){
        switch (index){
            case 1:
                return 10;
            case 2:
                return 15;
            case 3:
                return 20;
            case 4:
                return 25;
            case 5:
                return 30;
            case 0:
            default:
                return 5;
        }
    }

    private void playRing(){
        try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
            mRingTone = RingtoneManager.getRingtone(this, notification);
            mRingTone.setLooping(true);
            mRingTone.play();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("three_games", "播放鈴聲失敗");
        }
    }

    private void maxVolume(){
        mCurrentVolume = getAudioManager().getStreamVolume(AudioManager.STREAM_ALARM);
        getAudioManager().setStreamVolume(AudioManager.STREAM_ALARM, getAudioManager().getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);
    }

    private void restoreVolume(){
        getAudioManager().setStreamVolume(AudioManager.STREAM_ALARM, mCurrentVolume, 0);
    }

    private AudioManager getAudioManager(){
        return (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }

    private void stopPlay(){
        if (mRingTone != null && mRingTone.isPlaying()){
            mRingTone.stop();
        }
    }

    private String getLabel(){
        return mGameInfo.playGameType == ResGameInfo.GAME_TYPE_GAME ? "您需要做遊戲啦" : "您需要做量表啦";
    }

    public static void LAUNCH(Context context, ResGameInfo gameInfo){
        Intent intent = new Intent(context, RingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(PARAM_GAME_INFO, gameInfo);
        context.startActivity(intent);
    }
}