package com.yyrh.factory;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;

import com.yyrh.constant.RoleInfo;
import com.yyrh.networking.NetRequestApi;
import com.yyrh.sdk.SDKEntry;
import com.yyrh.sdk.YyrhSdkManager;
import com.yyrh.sdk.callback.PayOrderCallback;
import com.yyrh.sdk.callback.RealNameMsgCallback;
import com.yyrh.utils.YYLog;
import com.yythird.base.ChannelSdkCallback;

import org.json.JSONObject;

import java.util.HashMap;

import static com.yyrh.constant.Constants.CLICK_INTERVAL;
import static com.yyrh.constant.URLConstants.BASEURL;
import static com.yyrh.constant.URLConstants.PAYMENTWAY;

public class SdkManager {

    private static SdkManager manager = null;

    private ISdkOperation operation;



    private long clickLoginTime = 0L;

    private SdkManager(Context context) {
        operation = SdkFactory.GetSdk(context);
    }

    public static SdkManager defaultManager(Context context) {
        if (manager == null) {
            synchronized (SdkManager.class) {
                if (manager == null) {
                    manager = new SdkManager(context);
                }
            }
        }
        return manager;
    }



    /**
     * operation
     */

    public  void  onInitSuccess(Activity activity){
        operation = SdkFactory.GetSdk(activity);
    }

    public void login(Activity activity){

        if (System.currentTimeMillis() - clickLoginTime < CLICK_INTERVAL) {
            YYLog.i("调用登录接口太频繁，拦截此次操作");
            return;
        }
        clickLoginTime = System.currentTimeMillis();
        operation.login(activity);
    }

    public void uploadRoleInfo(String type, Activity activity, RoleInfo roleInfo){
        operation.uploadRoleInfo(type,activity,roleInfo);
    }
    public void reportAdEvent(String event, JSONObject json){
        operation.reportAdEvent(event,json);
    }

    public void pay(Activity activity, HashMap<String, Object> map){
        operation.pay(activity,map);
    }
    public void forcePay(final Activity activity, HashMap<String, Object> map){

        NetRequestApi.payOrderInit(PAYMENTWAY, map, activity, new PayOrderCallback() {
            @Override
            public void onPaySuccess(HashMap<String, Object> map) {

                SDKEntry.getSdkInstance().adSdkCallback.onCreateOrder(map);
                YyrhSdkManager.defaultManager(activity).yyrhPay(activity,map);
            }

            @Override
            public void onPayFailed(String msg) {
                SDKEntry.getSdkInstance().sdkCallback.onPayFinishCallback(false,msg);
            }
        });
    }
    public void exit(Activity activity){
        operation.exit(activity);
    }
    public void logOut(){
        operation.logOut();
    }
    public void getRealNameInfo(Activity activity, RealNameMsgCallback realNameMsgCallback){
        operation.getRealNameInfo(activity,realNameMsgCallback);
    }
    public void setSDKLogoutListener(Activity ac, ChannelSdkCallback sdkCallback){
        operation.setSDKLogoutListener(ac,sdkCallback);
    }


    /**
     * 生命周期
     */
    public void sdkOnStart(Activity activity){
        operation.sdkOnStart(activity);
    }

    public void sdkOnCreate(Activity activity, Bundle savedInstanceState){
        operation.sdkOnCreate(activity,savedInstanceState);
    }

    public void sdkOnPause(Activity activity){
        operation.sdkOnPause(activity);
    }

    public void sdkOnResume(Activity activity){
        operation.sdkOnResume(activity);
    }

    public void sdkOnRestart(Activity activity){
        operation.sdkOnRestart(activity);
    }

    public void sdkOnNewIntent(Activity acitivty, Intent intent){
        operation.sdkOnNewIntent(acitivty,intent);
    }

    public void sdkOnStop(Activity activity){
        operation.sdkOnStop(activity);
    }

    public void onConfigurationChanged(Configuration newConfigs){
        operation.onConfigurationChanged(newConfigs);
    }

    public void sdkOnDestroy(Activity activity){
        operation.sdkOnDestroy(activity);
    }


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

    public void onActivityResult(Activity ac,int requestCode, int resultCode, Intent data){
        operation.onActivityResult(ac,requestCode,resultCode,data);
    }

    public void onWindowFocusChanged(Activity activity, boolean hasFocus){
        operation.onWindowFocusChanged(activity,hasFocus);
    }


}
