package com.ewei.demo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ewei.sdk.base.BaseActivity;
import com.ewei.sdk.base.EweiConfig;
import com.ewei.sdk.base.EweiSdkInitializer;
import com.ewei.sdk.base.EweiSetting;
import com.ewei.sdk.listener.AccountListener;
import com.ewei.sdk.utility.SharedPreferencesUtils;

/**
 * 类：
 *
 * @author Jay
 * @date 2018/4/24.
 */
public class IndexActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtAppKey;
    private EditText mEtAppSecret;
    private EditText mEtAppDomain;
    private EditText mEtAccountInfo;
    private Button mBtnRunSdk;

    private SharedPreferencesUtils sharedPreferencesUtils;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        initView();
    }

    private void initView() {
        mEtAppKey = (EditText) findViewById(R.id.et_app_key);
        mEtAppSecret = (EditText) findViewById(R.id.et_app_secret);
        mEtAppDomain = (EditText) findViewById(R.id.et_domain_info);
        mEtAccountInfo = (EditText) findViewById(R.id.et_account_info);
        TextView mTvSdkVersion = (TextView) findViewById(R.id.tv_sdk_version);
        mTvSdkVersion.setText("SDK版本：" + EweiSetting.getVersionName());
        mBtnRunSdk = (Button) findViewById(R.id.btn_run_sdk);
        mBtnRunSdk.setOnClickListener(this);

        sharedPreferencesUtils = new SharedPreferencesUtils(this, "EWEI_SDK_SPU");

        String theAppKey = (String) sharedPreferencesUtils.getParam("mAppKey", "");
        String theAppSecret = (String) sharedPreferencesUtils.getParam("mAppSecret", "");
        String theDomain = (String) sharedPreferencesUtils.getParam("mDomain", "");
        String theAccount = (String) sharedPreferencesUtils.getParam("mAccount", "");

        mEtAppKey.setText(theAppKey);
        mEtAppSecret.setText(theAppSecret);
        mEtAppDomain.setText(theDomain);
        mEtAccountInfo.setText(theAccount);

        verifyStoragePermissions();
    }

    // Storage Permissions
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    public void verifyStoragePermissions() {
        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_run_sdk:
                initSdk();
                break;
            default:
                break;
        }
    }

    private void initSdk() {
        String appKeyStr = mEtAppKey.getText().toString();
        String appSecretStr = mEtAppSecret.getText().toString();
        String appDomain = mEtAppDomain.getText().toString();
        final String accountInfo = mEtAccountInfo.getText().toString();

        if (TextUtils.isEmpty(appKeyStr)) {
            showToast("AppKey不能为空");
            return;
        }

        if (TextUtils.isEmpty(appSecretStr)) {
            showToast("AppSecret不能为空");
            return;
        }

        if (TextUtils.isEmpty(appDomain)) {
            showToast("域名不能为空");
            return;
        }

        if (TextUtils.isEmpty(accountInfo)) {
            showToast("账户信息不能为空");
            return;
        }

        sharedPreferencesUtils.setParam("mAppKey", appKeyStr);
        sharedPreferencesUtils.setParam("mAppSecret", appSecretStr);
        sharedPreferencesUtils.setParam("mDomain", appDomain);
        sharedPreferencesUtils.setParam("mAccount", accountInfo);

        EweiConfig.setEweiKey(appKeyStr, appSecretStr);

        /**
         * 初始化
         */
        EweiSdkInitializer.initialize(getApplicationContext(), appDomain);

        /**
         * 是否调试日志
         */
        EweiSdkInitializer.enableDebug(true);

        showLoadingDialog(null);
        //初始化用户
        EweiConfig.initUserAccount(accountInfo, new AccountListener() {
            @Override
            public void onSuccess(boolean isSuccess) {
                hideLoadingDialog();
                if (isSuccess) {
                    Intent intent = new Intent(IndexActivity.this, MainActivity.class);
                    intent.putExtra("accountInfo", accountInfo);
                    startActivity(intent);
                }
            }
        });
    }
}
