package com.prankster.fx.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.prankster.fx.FXApp;
import com.prankster.fx.db.Bean;
import com.prankster.fx.db.Category;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class CommonUtils {

    public static void initStatusBar(Window window) {
        View decorView = window.getDecorView();
        int flags = decorView.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        decorView.setSystemUiVisibility(flags ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
    public static void initFont(TextView tv) {
        tv.setTypeface(FXApp.defaultFont);
    }
    public static void setStatusBarTextColor(Activity activity, boolean dark) {
        Window window = activity.getWindow();
        View decor = window.getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

    public static String getString(InputStream input) {
        try {
            char[] charArray = new char[input.available()];
            int count = 0;
            StringWriter stringWriter = new StringWriter();
            InputStreamReader inputStreamReader = new InputStreamReader(input);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            while ((count = bufferedReader.read(charArray)) != -1) {
                stringWriter.write(charArray, 0, count);
            }
            return stringWriter.toString();
        } catch (IOException exception) {
            return "";
        }

    }

    public static void setVolume(Context context, SeekBar seekBar) {
        AudioManager audioManager = (AudioManager)context.getSystemService(AppCompatActivity.AUDIO_SERVICE);
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        seekBar.setMax(maxVolume);
        seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
//                Log.d("----------","--------maxVolume="+maxVolume+"-------progress="+progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
    public static List<Category> getBean(String str) {

        try {
            List<Category> data = new ArrayList<Category>();
            JSONArray jsonArray = new JSONArray(str);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject it = jsonArray.getJSONObject(i);
                JSONArray listArray = it.getJSONArray("list");
                List<Bean> list = new ArrayList<>();
                for (int k = 0; k < listArray.length(); k++) {
                    JSONObject listBean = listArray.getJSONObject(k);
                    Bean bean = new Bean();
                    bean.setAudioUrl(listBean.getString("mp3Url"));
                    bean.setCovert(listBean.getString("preUrl"));
                    bean.setName(listBean.getString("title"));
                    list.add(bean);
                }

                Category newBean = new Category();
                newBean.setCovert(it.getString("categoryUrl"));
                newBean.setBeanList(list);
                newBean.setName(it.getString("categoryName"));
                data.add(newBean);

            }

            return data;
        } catch (JSONException jo) {
            return null;
        }
    }


    public static long getAudioDuration(Context context, Uri uri) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        long duration = 0;

        try {
            ContentResolver contentResolver = context.getContentResolver();
            retriever.setDataSource(context, uri);
            String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            duration = Long.parseLong(time);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return duration;
    }

    public static  String formatTime(int milliseconds) {

        int seconds = (milliseconds / 1000) % 60;
        int minutes = (milliseconds / (1000 * 60)) % 60;
        int hours = milliseconds / (1000 * 60 * 60);


        return String.format("%02d:%02d:%02d", minutes, seconds, milliseconds);
    }
}
