package com.gem.media.util;

import static android.media.AudioManager.AUDIOFOCUS_REQUEST_FAILED;

import android.content.Context;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.gem.media.splash.base.utils.ALog;

public class CAudioManagerUtils {
    private static final String TAG = "CAudioManagerUtils";

    private static final class CAudioManagerUtilsHolder {
        private static final CAudioManagerUtils manager = new CAudioManagerUtils();
    }

    public static CAudioManagerUtils getInstance() {
        return CAudioManagerUtilsHolder.manager;
    }

    private AudioManager mAudioManager;

    public AudioManager getAudioManager(Context context) {
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        }
        return mAudioManager;
    }

    private boolean isInCalling(Context context) {
        int mode = getAudioManager(context).getMode();
        return mode == AudioManager.MODE_IN_CALL || mode == AudioManager.MODE_IN_COMMUNICATION;
    }

    public boolean isCalling(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        int callState = telephonyManager.getCallState();
        return callState != TelephonyManager.CALL_STATE_IDLE || isInCalling(context);
    }

    private final AudioManager.OnAudioFocusChangeListener afChangeListener = focusChange -> {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_LOSS:
                ALog.e(TAG, "AudioManager.AUDIOFOCUS_LOSS = ");
                // 长时间失去焦点，应当停止播放并释放资源
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                ALog.e(TAG, "AudioManager.AUDIOFOCUS_LOSS_TRANSIENT = ");
                // 短暂失去焦点，暂停播放等待焦点恢复
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                ALog.e(TAG, "AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK = ");
                // 短暂失去焦点，可降低音量继续播放（“鸭子式”处理）
                break;
            case AudioManager.AUDIOFOCUS_GAIN:
                ALog.e(TAG, "AudioManager.AUDIOFOCUS_GAIN = ");
                // 获得焦点，恢复播放
                break;
        }
    };
    private AudioFocusRequest audioFocusRequest;

    public boolean isRequestAudioFocus(Context context) {
        boolean b = isCalling(context);
        ALog.e(TAG, "iscalling = " + b);
        if (b) {
            return true;
        }

        int result;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            AudioFocusRequest.Builder builder = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN);
            builder.setOnAudioFocusChangeListener(afChangeListener);
            audioFocusRequest = builder.build();
            result = getAudioManager(context).requestAudioFocus(audioFocusRequest);
        } else {
            result = getAudioManager(context).requestAudioFocus(afChangeListener,
                    AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN);
        }

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            ALog.e(TAG, "获得音频焦点");
        } else if (result == AUDIOFOCUS_REQUEST_FAILED) {
            ALog.e(TAG, "没有获得音频焦点");
        } else {
            ALog.e(TAG, "result  " + result);
        }
        return false;
    }


    public void cancelAudioFocus(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (null != audioFocusRequest) {
                getAudioManager(context).abandonAudioFocusRequest(audioFocusRequest);
                audioFocusRequest= null;
                mAudioManager = null;
                ALog.e(TAG, "abandonAudioFocusRequest");
            }
        } else {
            getAudioManager(context).abandonAudioFocus(afChangeListener);
            ALog.e(TAG, "abandonAudioFocus");
        }
    }

}
