package com.gs.apidemo;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gs.phone.api.audio.AudioRouteApi;
import com.gs.phone.api.call.BaseCallApi;
import com.gs.phone.context.PhoneContext;
import com.gs.phone.listener.CallStatusListener;

public class AudioRouteActivity extends BaseActivity {
    private static final String TAG = AudioRouteActivity.class.getSimpleName();
    private TextView resTv;
    private RingtoneManager ringtoneManager;
    private Ringtone mRingtone;
    private View curSelView;
    private CallStatusListener mCallStatusListener = new CallStatusListener() {
        @Override
        public void onHookEventChanged(int device, boolean isOffHook) {
            super.onHookEventChanged(device, isOffHook);
            Toast.makeText(AudioRouteActivity.this, "EHS headset change to " + (AudioRouteApi.isEhsOffHook() ? "offhook" : "onhook"),
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_route);

        resTv = (TextView) findViewById(R.id.tv_res);
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        ringtoneManager = new RingtoneManager(this);
        mRingtone = RingtoneManager.getRingtone(this, uri);

        mRingtone.play();

        resTv.setText("isVoiceUnknown: " + AudioRouteApi.isVoiceUnknown());
        BaseCallApi.addStatusListener(getPackageName(), mCallStatusListener.callback, PhoneContext.ListenType.HOOK_EVENT_STATUS, false);
    }

    private void selView(View view) {
        view.setSelected(true);
        if (curSelView != null) {
            curSelView.setSelected(false);
        }
        curSelView = view;
    }

    public void switchVoiceToSpeaker(View view) {
        selView(view);
        AudioRouteApi.switchVoiceToSpeaker();

        if (AudioRouteApi.isVoiceOnSpeaker()) {
            resTv.setText("Voice Speaker On");
        } else {
            Toast.makeText(this, "switch failed!", Toast.LENGTH_SHORT).show();
        }
    }

    public void switchVoiceToHandset(View view) {
        selView(view);
        AudioRouteApi.switchVoiceToHandset();

        if (AudioRouteApi.isVoiceOnHandset()) {
            resTv.setText("Voice Handset On");
        } else {
            Toast.makeText(this, "switch failed!", Toast.LENGTH_SHORT).show();
        }
    }


    public void switchVoiceToRJ9Headset(View view) {
        selView(view);
        AudioRouteApi.switchVoiceToRJ9Headset();

        if (AudioRouteApi.isVoiceOnRJ9Headset()) {
            resTv.setText("Voice RJ9 Headset On");
        } else {
            Toast.makeText(this, "switch failed!", Toast.LENGTH_SHORT).show();
        }
    }


    public void switchVoiceToBlueToothHeadset(View view) {
        selView(view);
        AudioRouteApi.switchVoiceToBlueToothHeadset();

        if (AudioRouteApi.isVoiceOnBlueToothHeadset()) {
            resTv.setText("Voice BlueTooth Headset On");
        } else {
            Toast.makeText(this, "switch failed!", Toast.LENGTH_SHORT).show();
        }
    }


    public void switchVoiceToHdmi(View view) {
        selView(view);
        AudioRouteApi.switchVoiceToHdmi();

        if (AudioRouteApi.isVoiceOnHdmi()) {
            resTv.setText("Voice Hdmi On");
        } else {
            Toast.makeText(this, "switch failed!", Toast.LENGTH_SHORT).show();
        }
    }


    public void switchVoiceToEarphone(View view) {
        selView(view);
        AudioRouteApi.switchVoiceToEarphone();

        if (AudioRouteApi.isVoiceOnEarphone()) {
            resTv.setText("Voice 3.5mm Earphone On");
        } else {
            Toast.makeText(this, "switch failed!", Toast.LENGTH_SHORT).show();
        }
    }


    public void switchVoiceToUsbHeadset(View view) {
        selView(view);
        AudioRouteApi.switchVoiceToUsbHeadset();

        if (AudioRouteApi.isVoiceOnUsbHeadset()) {
            resTv.setText("Voice UsbHeadset On");
        } else {
            Toast.makeText(this, "switch failed!", Toast.LENGTH_SHORT).show();
        }
    }

    public void setEhsOffHook(View view) {
        selView(view);

        if (AudioRouteApi.isVoiceOnRJ9Headset()) {
            AudioRouteApi.setEhsHookStatus(true);
        } else {
            Toast.makeText(this, "Failed! The voice must on the RJ9", Toast.LENGTH_SHORT).show();
        }
    }

    public void setEhsOnHook(View view) {
        selView(view);

        if (AudioRouteApi.isVoiceOnRJ9Headset()) {
            AudioRouteApi.setEhsHookStatus(false);
        } else {
            Toast.makeText(this, "Failed! The voice must on the RJ9", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onDestroy() {
        mRingtone.stop();
        if (mCallStatusListener != null) {
            BaseCallApi.removeStatusListener(mCallStatusListener.callback);
        }
        super.onDestroy();
    }
}
