package com.haircut.pranks.app.manager;

import android.Manifest;
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.haircut.pranks.app.HaircutApp;
import com.haircut.pranks.app.R;
import com.haircut.pranks.app.data.ChildData;
import com.haircut.pranks.app.data.ParentData;

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 Tools {

    public static int[] bgs = new int[]{R.drawable.rect_main_gradient,R.drawable.rect_main_gradient,R.drawable.rect_main_gradient,R.drawable.rect_main_gradient,R.drawable.rect_main_gradient};
    public static final  String KEY_LIST_COLOR_POS = "list_page_color_pos";
    public static final  String KEY_LIST_DATA = "list_page_data";
    public static final  String KEY_PLAY_DATA = "play_page_data";
    public static final  String KEY_PLAY_COLOR_POS = "play_page_color_pos";

    public static final int REQUEST_READ_STORAGE_PERMISSION = 5;
    public static final int REQUEST_PICK_AUDIO = -3;

    public static  String[]  permission_read_audio = {Manifest.permission.READ_MEDIA_AUDIO};

    public static String permission_record_audio = Manifest.permission.RECORD_AUDIO;
    public static String[] permission_store = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};


    public static void initFont(TextView tv) {
        tv.setTypeface(HaircutApp.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<ParentData> getBean(String str) {

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

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

                ParentData newBean = new ParentData();
                newBean.setCovert(it.getString("categoryUrl"));
                newBean.setChildDataList(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 float dpToPx(float dpValue) {
        float density = HaircutApp.app.getResources().getDisplayMetrics().density;
        return density * dpValue + 0.5f;
    }

}
