package com.prank.funny.pranks.tools;

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.prank.funny.pranks.FunnyApp;
import com.prank.funny.pranks.R;
import com.prank.funny.pranks.bean.Info;
import com.prank.funny.pranks.bean.MyBean;

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

    public static int[] bgs = new int[]{R.drawable.bg_home,R.drawable.bg_home,R.drawable.bg_home,R.drawable.bg_home,
            R.drawable.bg_home,R.drawable.bg_home};

    public static int[] childBgs = new int[]{R.drawable.bg_class_item,R.drawable.bg_class_item,R.drawable.bg_class_item,R.drawable.bg_class_item,
            R.drawable.bg_class_item,R.drawable.bg_class_item};


    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  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(FunnyApp.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);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

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

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

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

                MyBean newBean = new MyBean();
                newBean.setCovert(it.getString("categoryUrl"));
                newBean.setInfoList(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 = FunnyApp.app.getResources().getDisplayMetrics().density;
        return density * dpValue + 0.5f;
    }

}
