package com.yyrh.advertisement;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.yyrh.constant.RoleInfo;

import org.json.JSONObject;

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


public class Advertise {

    public void advertiseInitInApplication(Application application) {
    }

    public void advertiseInit(Activity activity) {
    }

    public void login(Activity activity,String account) {
    }

    public void active(Activity activity) {
    }

    public void registration(Activity activity,String account) {
    }

    public void callPayment(Activity activity, HashMap<String, Object> info) {
    }

    public void paymentFinish(Activity activity, HashMap<String, Object> info) {
    }
    public void uploadRoleInfo(Activity activity,String type,  RoleInfo roleInfo) {

    }
    public void onResume(Activity activity) {

    }

    public void onPause(Activity activity) {
    }
    public void onDestory(Activity activity) {
    }
    public void onExitGame(Activity activity) {
    }
    public Advertise() {
    }

    public void reportAdEvent(String event, JSONObject json){

    }
    public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults){
    }

    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 getParamFromCnfByName(Context context, String name) {
        String value = "";

        InputStream input_stream = null;
        try {
            input_stream = context.getAssets().open("YyrhAdParam.cnf");
        } catch (IOException e) {
            e.printStackTrace();
        }

        InputStreamReader inputStreamReader = new InputStreamReader(
                input_stream);

        BufferedReader reader = new BufferedReader(inputStreamReader);

        String tempString = null;

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

                if (tempString.contains(name)) {

                    value = tempString.substring(tempString.indexOf("=") + 1,
                            tempString.length());
                    break;
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return value;

    }

}
