package com.fq.channel.sdk.core.invoke;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.fq.channel.sdk.base.exception.NoSuchFunctionException;
import com.fq.channel.sdk.base.interfaces.PluginResult;
import com.fq.channel.sdk.base.interfaces.PluginResultHandler;
import com.fq.channel.sdk.base.utils.FqLog;

import java.util.Map;
/**
 * @Description: 反射渠道方法
 *
 * @Author: FLuty
 * @CreateDate: 2020/3/24 17:59
 */
public class ApiPlugin extends ApiReflectManager {

    protected String TAG = getClass().getSimpleName();


    private static byte[] sync = new byte[0];
    private static ApiPlugin instance = null;

    public static ApiPlugin getInstace() {
        if (instance == null) {
            synchronized (sync) {
                instance = new ApiPlugin();
            }
        }
        return instance;
    }

    private ApiPlugin() {

    }

    Handler mainHandler = new Handler(Looper.getMainLooper());

    public void post(Runnable r) {
        mainHandler.post(r);
    }


    public void init(Context context, PluginResultHandler cb) {
        if (cb == null) {
            FqLog.e(TAG, "====initialize====的回调函数为null,请初始化");
            return;
        }

        try {
            invoke(getDeclaredMethod("init", Context.class, PluginResultHandler.class),
                    context, cb);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "initialize NoSuchFunctionException");
            e.printStackTrace();
            cb.onHandlePluginResult(new PluginResult(PluginResult.Status.ERROR));
        }
    }


    public void login(Context context, PluginResultHandler cb) {

        if (cb == null) {
            FqLog.e(TAG, "====login====的回调函数为null,请初始化");
            return;
        }
        try {
            invoke(getDeclaredMethod("login", Context.class, PluginResultHandler.class),
                    context, cb);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "login NoSuchFunctionException");
            e.printStackTrace();
            cb.onHandlePluginResult(new PluginResult(PluginResult.Status.ERROR));
        }

    }

    public void pay(Context context, Map map, PluginResultHandler cb) {

        if (cb == null) {
            FqLog.e(TAG, "====pay====的回调函数为null,请初始化");
            return;
        }
        try {
            invoke(getDeclaredMethod("pay", Context.class, Map.class, PluginResultHandler.class),
                    context, map, cb);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "login NoSuchFunctionException");
            e.printStackTrace();
            cb.onHandlePluginResult(new PluginResult(PluginResult.Status.ERROR));
        }

    }

    public void exit(Context context, final PluginResultHandler cb) {
        if(cb == null) {
            FqLog.e(TAG, "====exit====的回调函数为null,请初始化");
            return;
        }
        try {
            invoke(getDeclaredMethod("exit", Context.class, PluginResultHandler.class),
                    context, cb);
        } catch (NoSuchFunctionException e) {
            FqLog.printStackTrace(e);
            cb.onHandlePluginResult(new PluginResult(PluginResult.Status.CANCEL));
        }
    }

    public void setRoleInfo(Bundle data) {
        try {
            invoke(getDeclaredMethod("setRoleInfo", Bundle.class), data);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====setRoleInfo====没有实现该函数");
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        try {
            invoke(getDeclaredMethod("onCreate" ,Bundle.class), savedInstanceState);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onCreate====没有实现该函数");
        }
    }

    public void onSaveInstanceState(Bundle outState) {
        try {
            invoke(getDeclaredMethod("onSaveInstanceState", Bundle.class), outState);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onSaveInstanceState====没有实现该函数");
        }
    }

    public void onRestart() {
        try {
            invoke(getDeclaredMethod("onRestart"));
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onRestart====没有实现该函数");
        }
    }

    public void onStart() {
        try {
            invoke(getDeclaredMethod("onStart"));
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onStart====没有实现该函数");
        }
    }

    public void onResume() {
        try {
            invoke(getDeclaredMethod("onResume"));
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onResume====没有实现该函数");
        }
    }

    public void onPause() {
        try {
            invoke(getDeclaredMethod("onPause"));
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onPause====没有实现该函数");
        }
    }

    public void onStop() {
        try {
            invoke(getDeclaredMethod("onStop"));
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onStop====没有实现该函数");
        }
    }

    public void onNewIntent(Intent intent) {
        try {
            invoke(getDeclaredMethod("onNewIntent", Intent.class), intent);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onNewIntent====没有实现该函数");
        }
    }

    public void onDestroy() {
        try {
            invoke(getDeclaredMethod("onDestroy"));
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onDestroy====没有实现该函数");
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            invoke(getDeclaredMethod("onActivityResult", int.class, int.class, Intent.class),
                    requestCode, resultCode, data);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onActivityResult====没有实现该函数");
        }
    }

    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {
        try {
            invoke(getDeclaredMethod("onActivityResult", int.class, String.class, Intent.class),
                    requestCode, permissions, grantResults);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onActivityResult====没有实现该函数");
        }
    }

    public void attachBaseContext(Context context) {
        try {
            invoke(getDeclaredMethod("attachBaseContext", Context.class), context);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====attachBaseContext====没有实现该函数");
        }
    }

    public void onApplicationCreate(Context context) {
        try {
            invoke(getDeclaredMethod("onApplicationCreate", Context.class), context);
        } catch (NoSuchFunctionException e) {
            FqLog.e(TAG, "====onApplicationCreate====没有实现该函数");
        }
    }

}
