package com.sobot.common.utils;

import android.content.Context;
import android.graphics.Color;
import android.os.Environment;

import com.sobot.utils.SobotStringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

public class SobotResourceUtils {
    public static Map<String,String> resource;

    public static void setResource(Map<String, String> resource) {
        SobotResourceUtils.resource = resource;
    }

    public static Map<String, String> getResource() {
        return resource;
    }

    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 getDrawableId(Context context, String name) {
        return SobotResourceUtils.getIdByName(context, "drawable", name);
    }

    public static String getResString(Context context, String name) {
        String value ="";
        if(resource!=null){
            value = resource.get(name);
        }
        if(SobotStringUtils.isEmpty(value)){
            value = context.getResources().getString(getResStrId(context, name));
        }
        return value;
    }

    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();
    }
    /**
     * 写入content到fileName
     *
     * @param fileName 文件路径
     * @param content  写入的内容
     * @param append   是否为append
     */
    public static void writeStringToFile(String fileName, String content, boolean append) {
        try {
            checkFilePath(fileName);
            //使用true，即进行append file
            FileWriter fileWritter = new FileWriter(fileName, append);
            fileWritter.write(content);
            fileWritter.close();

            System.out.println("finish");

        } catch (IOException e) {

            e.printStackTrace();

        }
    }


    public static void checkFilePath(String fileName) {


        File file = new File(fileName.substring(0, fileName.lastIndexOf("/") + 1));

        if (!file.isDirectory()) {
            file.mkdirs();
        }
        file = new File(fileName);

        if (!file.isFile()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}