package com.yyxx.eyuangame.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import cn.yyxx.eyuangame.base.EYuanGame;
import cn.yyxx.eyuangame.base.entity.SdkChargeInfo;
import cn.yyxx.eyuangame.base.entity.SdkEvent;
import cn.yyxx.eyuangame.base.entity.SdkRoleInfo;
import cn.yyxx.eyuangame.base.internal.ICallback;
import cn.yyxx.support.HostModelUtils;

/**
 * @author #Suyghur.
 * Created on 2021/07/12
 */
public class DemoActivity extends Activity implements View.OnClickListener {

    private List<Item> events = new ArrayList<Item>() {
        {
            add(new Item(0, "00 用户登录"));
            add(new Item(1, "01 用户登出"));
            add(new Item(2, "02 角色创建"));
            add(new Item(3, "03 角色登录"));
            add(new Item(4, "04 角色升级"));
            add(new Item(5, "05 定额充值"));
            add(new Item(6, "06 模拟Facebook标准事件打点（玩家首次完成新手引导）"));
            add(new Item(7, "07 模拟Facebook自定义事件打点（玩家首次完成结缘）"));
        }
    };
    private CacheInfo.RoleInfo cacheRoleInfo = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        EYuanGame.getInstance().initialize(this, true, new ICallback() {
            @Override
            public void onResult(int code, @NonNull String result) {
                toast(result);
            }
        });
    }

    private void initView() {
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        for (Item event : events) {
            Button button = new Button(this);
            button.setId(event.id);
            button.setTag(event.id);
            button.setText(event.name);
            button.setOnClickListener(DemoActivity.this);
            layout.addView(button);
        }
        ScrollView scrollView = new ScrollView(this);
        scrollView.addView(layout);
        setContentView(scrollView);
    }

    private SdkRoleInfo getSdkRoleInfo() {
        SdkRoleInfo roleInfo = new SdkRoleInfo();
        roleInfo.setUserId(EYuanGame.getInstance().getCurrentUserId());
        roleInfo.setRoleId(cacheRoleInfo.roleId);
        roleInfo.setRoleName(cacheRoleInfo.roleName);
        roleInfo.setRoleLevel(cacheRoleInfo.roleLevel);
        roleInfo.setRoleCTime(cacheRoleInfo.roleCTime);
        roleInfo.setServerId(cacheRoleInfo.serverCode);
        roleInfo.setServerName(cacheRoleInfo.serverName);
        roleInfo.setBalance(cacheRoleInfo.balance);
        roleInfo.setVipLevel(cacheRoleInfo.vipLevel);
        roleInfo.setTotalPurchase(cacheRoleInfo.totalPurchase);
        return roleInfo;
    }

    private SdkChargeInfo getSdkChargeInfo() {
        long ts = System.currentTimeMillis();
        SdkChargeInfo chargeInfo = new SdkChargeInfo();
        chargeInfo.setUserId(EYuanGame.getInstance().getCurrentUserId());
        chargeInfo.setRoleId(cacheRoleInfo.roleId);
        chargeInfo.setRoleName(cacheRoleInfo.roleName);
        chargeInfo.setRoleLevel(cacheRoleInfo.roleLevel);
        chargeInfo.setRoleCTime(cacheRoleInfo.roleCTime);
        chargeInfo.setServerId(cacheRoleInfo.serverCode);
        chargeInfo.setServerName(cacheRoleInfo.serverName);
        chargeInfo.setAmount(100);
        chargeInfo.setProductId("vn.ftsx.22000vnd");
        chargeInfo.setProductName("80NB");
        chargeInfo.setProductDesc("80NB");
        chargeInfo.setCpOrderId("order_" + ts);
        chargeInfo.setCpExt("cp_ext_info||" + chargeInfo.getCpOrderId());
        chargeInfo.setCpPayTime(ts / 100);
        chargeInfo.setCpPaySign("test123");
        return chargeInfo;
    }

    @Override
    protected void onStart() {
        super.onStart();
        EYuanGame.getInstance().onStart(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        EYuanGame.getInstance().onRestart(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        EYuanGame.getInstance().onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EYuanGame.getInstance().onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EYuanGame.getInstance().onStop(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EYuanGame.getInstance().onDestroy(this);
        //如果要杀进程需要在onDestroy中处理
        System.exit(0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        EYuanGame.getInstance().onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        EYuanGame.getInstance().onNewIntent(this, intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            EYuanGame.getInstance().openExitView(this, (code, result) -> {
                if (code == 0) {
                    // 如果要杀进程需要在onDestroy中处理，不然会不执行SDK的onDestroy方法
                    finish();
                }
            });
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch ((Integer) v.getTag()) {
            case 0: {
                EYuanGame.getInstance().login(this, true, new ICallback() {
                    @Override
                    public void onResult(int code, @NonNull String result) {
                        toast(result);
                        if (code == 0) {
                            cacheRoleInfo = CacheInfo.getDemoRoleInfo(DemoActivity.this, EYuanGame.getInstance().getCurrentUserId());
                        }
                    }
                });
                break;
            }
            case 1: {
                EYuanGame.getInstance().logout(this, new ICallback() {
                    @Override
                    public void onResult(int code, @NonNull String result) {
                        if (code == 0) {
                            EYuanGame.getInstance().login(DemoActivity.this, false, new ICallback() {
                                @Override
                                public void onResult(int code2, @NonNull String result2) {
                                    toast(result2);
                                    if (code2 == 0) {
                                        cacheRoleInfo = CacheInfo.getDemoRoleInfo(DemoActivity.this, EYuanGame.getInstance().getCurrentUserId());
                                    }
                                }
                            });
                        }
                    }
                });
                break;
            }
            case 2: {
                cacheRoleInfo = null;
                cacheRoleInfo = CacheInfo.setDemoRoleInfo(this, EYuanGame.getInstance().getCurrentUserId());
                EYuanGame.getInstance().roleCreate(this, getSdkRoleInfo());
                break;
            }
            case 3:
                EYuanGame.getInstance().roleLauncher(this, getSdkRoleInfo());
                break;
            case 4:
                EYuanGame.getInstance().roleLevelUp(this, getSdkRoleInfo());
                break;
            case 5: {
                EYuanGame.getInstance().charge(this, getSdkChargeInfo(), true, new ICallback() {
                    @Override
                    public void onResult(int code, @NonNull String result) {
                        toast(result);
                    }
                });
            }
            case 6: {
                //fb标准事件
                SdkEvent sdkEvent = new SdkEvent();
                sdkEvent.setEventName("tutorial");
                sdkEvent.setStandard(true);
                sdkEvent.setFbAliasName("fb_mobile_tutorial_completion");
                EYuanGame.getInstance().linkingEvent(this, sdkEvent);
                break;
            }
            case 7: {
                //fb标准事件
                SdkEvent sdkEvent = new SdkEvent();
                sdkEvent.setEventName("finish_marry");
                sdkEvent.setStandard(false);
                sdkEvent.setFbAliasName("");
                EYuanGame.getInstance().linkingEvent(this, sdkEvent);
                break;
            }
        }
    }

    private void toast(String msg) {
        Toast.makeText(DemoActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
}
