package com.haven.wallpapers.Utils;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.haven.wallpapers.MyApp;
import com.haven.wallpapers.jsonbean.Data;

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.Reader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class ReadFile {


    public static String getCovertStr(InputStream stream) {
        String covertStr = "";
        try {
            StringWriter writer = new StringWriter();
            char[] buffer = new char[stream.available()];
            Reader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
            int a = 0;
            while ((a = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, a);
            }
            covertStr = writer.toString();
        } catch (IOException e) {
            return covertStr;
        }
        return covertStr;
    }

    public static int dp2Px(int dp)  {
        float scale = MyApp.mAppContext.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }


    public static List<Data> getData( String jsonStr)    {


        try {
            JSONArray jsonArray = new JSONArray(jsonStr);
            List<Data> data = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                try {
                    JSONObject get = jsonArray.getJSONObject(i);
                    String textDescription = get.optString("alt_description");

                    // Extract links object and its properties
                    JSONObject links = get.optJSONObject("links");
                    String download = links.optString("download");
                    String downloadLocation = links.optString("download_location");
                    String html = links.optString("html");

                    // Extract urls object and its properties
                    JSONObject urls = get.optJSONObject("urls");
                    String full = urls.optString("full");
                    String raw = urls.optString("raw");
                    String regular = urls.optString("regular");
                    String small = urls.optString("small");
                    String thumb = urls.optString("thumb");

                    // Extract user object and its properties
                    JSONObject users = get.optJSONObject("user");
                    String portfolioUrl = users.optString("portfolio_url");
                    String name = users.optString("name");
                    String headerLarge = users.optString("header_large");
                    String headerMedium = users.optString("header_medium");
                    String headerSmall = users.optString("header_small");
                    String userHtml = users.optString("authorHtml");
                    String host = "https://unsplash.com/photos/";

                    // Extract the ID from the download URL
                    int indexOf = download.indexOf("/download");
                    String substring = download.substring(host.length(), indexOf);

                    // Create a new Data object and populate it with the extracted values
                    Data bean = new Data();
                    bean.setImId(substring);
                    bean.setDescription(textDescription);
                    bean.setFullUrl(download);
                    bean.setPreviewUrl1080(regular);
                    bean.setPreviewUrl400(small);
                    bean.setPreviewUrl200(thumb);


                    // Add the Data object to the result list
                    data.add(bean);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }



            return data;
        }catch (JSONException exception){
            return null;
        }
        // Initialize a new JSON array from the input string


        // Loop through the JSON array and extract data


        // Return the list of Data objects

    }


    public static void initFull(Activity activity, boolean light) {
        Window window = activity.getWindow();
        View decorView = window.getDecorView();
        View rootView = decorView.getRootView();
        if (light) {


        } else {

            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}
