package com.quyunshuo.androidbaseframemvvm.base.utils;

import static androidx.core.content.ContextCompat.getSystemService;

import android.annotation.SuppressLint;
import android.app.XzjhSystemManager;
import android.media.AudioManager;
import android.util.Log;

import com.quyunshuo.androidbaseframemvvm.base.BaseApplication;

public class XzjhSystemManagerUtlis {
    AudioManager audioManager = null;
    private static volatile XzjhSystemManagerUtlis singleton;
    XzjhSystemManager mMamager = null;


    private XzjhSystemManagerUtlis() {
        mMamager = (XzjhSystemManager) BaseApplication.context.getSystemService("xzjh_server");
        audioManager = (AudioManager) BaseApplication.context.getSystemService(BaseApplication.context.AUDIO_SERVICE);
    }


    public static XzjhSystemManagerUtlis getInstance() {
        if (singleton == null) {
            synchronized (XzjhSystemManagerUtlis.class) {
                if (singleton == null) {
                    singleton = new XzjhSystemManagerUtlis();
                }
            }
        }
        return singleton;
    }

    public void xzjhSetBacklight(int backlight) {
        if (mMamager == null) {
            return;
        }
        mMamager.xzjhSetBacklight(backlight);
    }

    public int xzjhGetBacklight() {
        if (mMamager == null) {
            return 0;
        }
        return mMamager.xzjhGetBacklight();
    }

    // 设置音量（范围 0~max）
    public void setVolume(int streamType, int volume) {

        int max = audioManager.getStreamMaxVolume(streamType);
        volume = Math.min(volume, max);
        audioManager.setStreamVolume(streamType, volume, 0);
    }

    // 获取当前音量百分比（0.0f ~ 1.0f）
    public int getVolumePercentage(int streamType) {

        int current = audioManager.getStreamVolume(streamType);
        Log.e("backinfo", "current:" + current);
        return current;
       /* int max = audioManager.getStreamMaxVolume(streamType);
        return max == 0 ? 0 : (float) current / max;*/
    }

    public int getStreamMaxVolume(int streamType) {
        return audioManager.getStreamMaxVolume(streamType);
    }

    // 显示系统音量面板
    public void showVolumePanel(int streamType) {

        audioManager.adjustStreamVolume(
                streamType,
                AudioManager.ADJUST_SAME,
                AudioManager.FLAG_SHOW_UI
        );
    }

    public void xzjhSetSystemBarEnable(boolean enable) {
        if (mMamager == null) {
            return;
        }
        mMamager.xzjhSetSystemBarEnable(enable);
    }


}