package com.sobot.widget.ui.utils;

import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import androidx.core.content.ContextCompat;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class SobotResourceUtils {

    public static int getIdByName(Context context, String className,
                                  String resName) {
        context = context.getApplicationContext();
        String packageName = context.getPackageName();
        int indentify = context.getResources().getIdentifier(resName,
                className, packageName);
        return indentify;
    }

    public static int getResLayoutId(Context context, String name) {
        return getIdByName(context, "layout", name);
    }

    public static int getResStrId(Context context, String name) {
        return getIdByName(context, "string", name);
    }

    public static int getResColorId(Context context, String name) {
        return getIdByName(context, "color", name);
    }

    public static int getResColorValue(Context context, String name) {
        return ContextCompat.getColor(context, getResColorId(context, name));
    }

    public static int getDrawableId(Context context, String name) {
        return SobotResourceUtils.getIdByName(context, "drawable", name);
    }

    public static String getResString(Context context, String name) {
        return context.getResources().getString(getResStrId(context, name));
    }

    public static int getResId(Context context, String name) {
        return SobotResourceUtils.getIdByName(context, "id", name);
    }

    //颜色id 转16进制颜色码
    public static String getColorById(Context context, String name) {
        if (context == null) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        int color = context.getResources().getColor(getResColorId(context, name));

        stringBuffer.append("#");
        stringBuffer.append(Integer.toHexString(Color.alpha(color)));
        stringBuffer.append(Integer.toHexString(Color.red(color)));
        stringBuffer.append(Integer.toHexString(Color.green(color)));
        stringBuffer.append(Integer.toHexString(Color.blue(color)));
        return stringBuffer.toString();
    }

    /**
     * 从External文件目录下读取文件
     *
     * @param filePathName 要读取的文件的路径+文件名
     * @return
     * @throws IOException
     */
    public static String readExternal(String filePathName) throws IOException {

        StringBuffer stringBuffer = new StringBuffer();

        // 获取External的可用状态
        String storageState = Environment.getExternalStorageState();

        if (storageState.equals(Environment.MEDIA_MOUNTED)) {
            // 当External的可用状态为可用时

            // 打开文件输入流
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(filePathName);
                InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
                BufferedReader br = new BufferedReader(reader);
                String line;
                while ((line = br.readLine()) != null) {
                    stringBuffer.append(line);
                }
                br.close();
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return stringBuffer.toString();
    }
}