package com.jld.vod.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SeekBar;

import com.jld.vod.R;
import com.jld.vod.model.bean.LanguageBean;
import com.jld.vod.view.widget.VerticalSeekBar;
import com.xuexiang.xui.adapter.simple.XUISimpleAdapter;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.popupwindow.popup.XUIListPopup;
import com.xuexiang.xui.widget.popupwindow.popup.XUIPopup;

import java.util.List;

import static android.widget.ListPopupWindow.MATCH_PARENT;

/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2020/9/15 16:48
 */
public class PopupUtils {

    private static boolean flag;

    /**
     * 亮度调节设置
     * @param context
     * @param v
     */
    public static void popupUtilsbrightness(Context context, View v)
    {
        XUIPopup mNormalPopup = new XUIPopup(context);
        int brightness = BrightnessUtils.INSTANCE.getSystemBrightness(context);
        VerticalSeekBar mSeekBar = new VerticalSeekBar(context);
        mSeekBar.setLayoutParams(mNormalPopup.generateLayoutParam(
                MATCH_PARENT,
                DensityUtils.dp2px(250)
        ));

        Resources mRes = context.getResources();
        Drawable dw = mRes.getDrawable(R.drawable.seekbar_item);
        int padding = DensityUtils.dp2px(20);
        mSeekBar.setPadding(padding, padding, padding, padding);
        mSeekBar.setProgressDrawable(dw);
        mSeekBar.setProgress(brightness);
        mSeekBar.setMax(255);
        //背光监听
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                BrightnessUtils.INSTANCE.setSystemBrightess(context,progress);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });

        mNormalPopup.setContentView(mSeekBar);
        mNormalPopup.setAnimStyle(XUIPopup.ANIM_GROW_FROM_CENTER);

        mNormalPopup.setPreferredDirection(XUIPopup.DIRECTION_TOP);
        mNormalPopup.show(v);
    }
    /**
     * 亮度调节设置
     * @param context
     * @param v
     */
    public static void popupAppUtilsbrightness(Activity context, View v)
    {
        XUIPopup mNormalPopup = new XUIPopup(context);
        int brightness = BrightnessUtils.INSTANCE.getAppSystemBrightness(context);
        VerticalSeekBar mSeekBar = new VerticalSeekBar(context);
        mSeekBar.setLayoutParams(mNormalPopup.generateLayoutParam(
                MATCH_PARENT,
                DensityUtils.dp2px(250)
        ));

        Resources mRes = context.getResources();
        Drawable dw = mRes.getDrawable(R.drawable.seekbar_item);
        int padding = DensityUtils.dp2px(20);
        mSeekBar.setPadding(padding, padding, padding, padding);
        mSeekBar.setProgressDrawable(dw);
        mSeekBar.setProgress(brightness);
        mSeekBar.setMax(255);
        //背光监听
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                BrightnessUtils.INSTANCE.setBrightness(context,progress);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });

        mNormalPopup.setContentView(mSeekBar);
        mNormalPopup.setAnimStyle(XUIPopup.ANIM_GROW_FROM_CENTER);

        mNormalPopup.setPreferredDirection(XUIPopup.DIRECTION_TOP);
        mNormalPopup.show(v);
    }
    /**
     * 声音调节
     * @param context
     * @param v
     */
    public static void popupUtilsVolume(Context context, View v)
    {

        //获取系统的Audio管理者
        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        //最大音量
        int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        //当前音量
        int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);


        XUIPopup mNormalPopup = new XUIPopup(context);


        VerticalSeekBar mSeekBar = new VerticalSeekBar(context);
        mSeekBar.setLayoutParams(mNormalPopup.generateLayoutParam(
                MATCH_PARENT,
                DensityUtils.dp2px(250)
        ));

        Resources mRes = context.getResources();
        Drawable dw = mRes.getDrawable(R.drawable.seekbar_item);
        int padding = DensityUtils.dp2px(20);
        mSeekBar.setPadding(padding, padding, padding, padding);
        mSeekBar.setProgressDrawable(dw);
        mSeekBar.setProgress(currentVolume);
        mSeekBar.setMax(maxVolume);
        //背光监听
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //设置媒体音量为当前seekbar进度
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });

        mNormalPopup.setContentView(mSeekBar);
        mNormalPopup.setAnimStyle(XUIPopup.ANIM_GROW_FROM_CENTER);

        mNormalPopup.setPreferredDirection(XUIPopup.DIRECTION_TOP);
        mNormalPopup.show(v);
    }

    /**
     * 语言选择
     * @param context
     * @param v
     * @param language
     */
    public static boolean popupUtilsLanguage(Context context, View v, String[] language, List<LanguageBean> languageList)
    {

        XUISimpleAdapter adapter = XUISimpleAdapter.create(context, language);
        XUIListPopup mListPopup = new XUIListPopup(context, adapter);
        mListPopup.create(DensityUtils.dp2px(150), DensityUtils.dp2px(350), new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               // ToastUtils.toast("Item " + languageList.get(i).getCountryLanguage());
                SharedPrefUtils.putString(context,"language_lid",languageList.get(i).getLid()+"");
                mListPopup.dismiss();
                flag = true;
            }
        });

        mListPopup.setAnimStyle(XUIPopup.ANIM_GROW_FROM_CENTER);
        mListPopup.setPreferredDirection(XUIPopup.DIRECTION_TOP);
        mListPopup.show(v);
        return flag;
    }
}
