package cn.yyxx.commsdk.impl.merge.channel.huawei;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

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

public class RealAssertUtils {
    public static String getParamCnfValuebyKey(Context context,
                                               String filename, String key) {
        try {
            return readAssertFileWithKey(context, filename, key);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    public static String readAssertFileWithKey(Context context,
                                               String file_name, String key) throws IOException {
        String web_str = "";

        InputStream input_stream = context.getAssets().open(file_name);

        InputStreamReader inputStreamReader = new InputStreamReader(
                input_stream);

        BufferedReader reader = new BufferedReader(inputStreamReader);

        String tempString = null;

        while ((tempString = reader.readLine()) != null) {

            if (tempString.contains(key)) {

                web_str = tempString.substring(tempString.indexOf("=") + 1,
                        tempString.length());
                break;
            }
        }
        reader.close();

        return web_str;
    }

    public static String GetApplicationMetaData(Context context, String metaName) {
        Object metaData = "";

        if (null == metaName || metaName.isEmpty()) {

            return "";
        }
        try {
            ApplicationInfo info = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            metaData = info.metaData.get(metaName);
            // 是否有引用
            if (null != metaData && metaData.toString().contains("ref=")) {
                String newMetaName = metaData.toString().split("=")[1];
                metaData = GetApplicationMetaData(context, newMetaName);
            }
        } catch (PackageManager.NameNotFoundException e) {

        }
        if (null == metaData) {
            metaData = "";
        }
        return metaData.toString();
    }

    public static String getSharedPreferences(Context context,String head,String key){
        SharedPreferences sp = context.getSharedPreferences(head, Activity.MODE_PRIVATE);
        return  sp.getString(key, "") ;
    }


    public static void saveSharedPreferences( Context context,String head, String key, String value) {
        SharedPreferences sp = context
                .getSharedPreferences(head,
                        Activity.MODE_PRIVATE);
        // 实例化SharedPreferences.Editor对象（第二步）
        SharedPreferences.Editor editor = sp.edit();
        // 用putString的方法保存数据
        editor.putString(key, value);
        // 提交当前数据
        editor.commit();
    }
}
