package com.sheep.gamegroup.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 游戏认证
 * Created by ljy on 2018/9/12.
 */

public class CertificationUtil {

    private static CertificationUtil certificationUtil;

    public static CertificationUtil newInstance(){
        if(certificationUtil == null)
            certificationUtil = new CertificationUtil();
        return certificationUtil;
    }


    /**
     * 退出app
     * @param errorMsg
     */
    public void showCertificationErrorAndExitApp(final String errorMsg, long skip_time) {
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                SheepApp.getInstance().setGamePackgeName("");
                SheepApp.getInstance().setGameCode("");
                SheepApp.getInstance().setGameFlag(false);
                G.showToast(errorMsg);
                SheepApp.getInstance().quit();
//                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            }
        }, skip_time);
    }
    /**
     * 跳转回，游戏
     */
    public void doCertification(Context context, String packgeName, String ext){
        try {
            Log.e("game_-packgename---"+packgeName,"");
            Intent intent = new Intent();
            intent.setClassName(packgeName, "com.kfzs.duanduan.data.observer.helper.WhistleActivity");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(context.getString(R.string.kfzs_duanduan_datashare_certification_key), SheepApp.getInstance().getGameCode());

            if (TextUtils.isEmpty(ext)) {
                intent.putExtra(context.getString(R.string.kfzs_duanduan_datashare_package_extras), "");
            } else if("cancel".equals(ext)){
                intent.putExtra(context.getString(R.string.kfzs_duanduan_datashare_certification_cancel), "cancel");
            }else  {
                intent.putExtra(context.getString(R.string.kfzs_duanduan_datashare_package_extras), ext);
            }
            context.startActivity(intent);
            showCertificationErrorAndExitApp("", 100);
        } catch (Exception e) {
            e.printStackTrace();
            showCertificationErrorAndExitApp("认证失败", 100);
        }
    }
    /**
     *验证获取数据
     */
    public void authCerticicationCode(final Context context){
        ViewUtil.newInstance().showProgress((Activity) context);
        SheepApp.getInstance().getNetComponent().getApiService().verifactionAuth(SheepApp.getInstance().getGameCode())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        ViewUtil.newInstance().hideProgress((Activity) context);
                        if(baseMessage != null && baseMessage.getData() != null){
                            Log.e("data-------","");
                            doCertification(context, SheepApp.getInstance().getGamePackgeName(), baseMessage.getData()+"");
                        }else {
                            CertificationUtil.newInstance().showCertificationErrorAndExitApp("请求授权错误 " + baseMessage.getMsg(),1000);
                        }

                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        ViewUtil.newInstance().hideProgress((Activity) context);
                        CertificationUtil.newInstance().showCertificationErrorAndExitApp("请求授权错误 " + baseMessage.getMsg(),1000);
                    }
                });
    }
}
