package cn.zhios.zhiying_base_widget_example;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.trade.biz.context.AlibcTradeResult;
import com.alibaba.baichuan.trade.biz.core.taoke.AlibcTaokeParams;
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.alibaba.baichuan.trade.common.utils.AlibcLogger;

import java.util.HashMap;

public class TestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        TextView aliBcTV = findViewById(R.id.tv_aliBc);
        AlibcTradeSDK.asyncInit(this.getApplication(), new AlibcTradeInitCallback() {
            @Override
            public void onSuccess() {
                Toast.makeText(TestActivity.this, "阿里百川注册成功", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(TestActivity.this, "阿里百川注册失败" + s, Toast.LENGTH_LONG).show();
            }
        });

        aliBcTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlibcLogin.getInstance().showLogin(new AlibcLoginCallback() {
                    @Override
                    public void onSuccess(int i, String s, String s1) {
                        Toast.makeText(TestActivity.this, "授权成功" + s, Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(TestActivity.this, "登陆失败" + s, Toast.LENGTH_LONG).show();
                    }
                });
            }
        });

        findViewById(R.id.tv_open_detail).setOnClickListener(new View.OnClickListener() {
            AlibcShowParams showParams = new AlibcShowParams();
            @Override
            public void onClick(View v) {
//                // 以显示传入url的方式打开页面（第二个参数是套件名称）
//                AlibcTrade.openByUrl(TestActivity.this, "", "http://www.izhim.com/comm/tb_callback.php?_ctrl=domain&domain_state=MTI2&domain_return_url=aHR0cDovLzE5Mi4xNjguMC4xMTM6NTAwMC9hcGkvdjEvdGFvYmFvL2F1dGgvY2FsbGJhY2s%2FdGltZT0xNjAyNDk4MjA3", new WebView(TestActivity.this),
//                        new WebViewClient(), new WebChromeClient(), showParams,
//                      new AlibcTaokeParams("","",""), new HashMap<String,String>(), new AlibcTradeCallback() {
//                            @Override
//                            public void onTradeSuccess(AlibcTradeResult tradeResult) {
//                                AlibcLogger.i("", "request success");
//                            }
//                            @Override
//                            public void onFailure(int code, String msg) {
//                                AlibcLogger.e("", "code=" + code + ", msg=" + msg);
//                                if (code == -1) {
//                                    Toast.makeText(TestActivity.this, msg, Toast.LENGTH_SHORT).show();
//                                }
//                            }
//                        });

                if (!AlibcLogin.getInstance().isLogin()) {

                    AlibcLogin.getInstance().showLogin(new AlibcLoginCallback() {
                        @Override
                        public void onSuccess(int i, String s, String s1) {
                            startActivity(new Intent(TestActivity.this,WebActivity.class));
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Log.d("阿里百川授权失败",s);
                        }
                    });
                } else {
                    startActivity(new Intent(TestActivity.this,WebActivity.class));
                }


            }
        });

    }
}