package com.taikor.investment.modules.customer;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.taikor.investment.R;
import com.taikor.investment.base.BaseActivity;
import com.taikor.investment.event.BooleanEvent;
import com.taikor.investment.mylibrary.TAnalytics;
import com.taikor.investment.utils.GetDeviceId;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 顾客详情
 * Created by Any on 2017/5/4.
 */
public class CustomerDescActivity extends BaseActivity {

    @BindView(R.id.wv_customer_desc)
    WebView wvCustomerDesc;

    private String itemId, customerId, fromPage;
    private ProgressDialog progressDialog;

    @Override
    public int getContentViewId() {
        return R.layout.activity_customer_desc;
    }

    @Override
    public void initView() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.progress_hint));
        progressDialog.setCancelable(true);

        Intent intent = getIntent();
        itemId = intent.getStringExtra("itemId");
        customerId = intent.getStringExtra("customerId");
        fromPage = intent.getStringExtra("from");
    }

    @Override
    public void initData() {
        //支持App内部JavaScript交互
        WebSettings settings = wvCustomerDesc.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);

        wvCustomerDesc.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressDialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressDialog.dismiss();
            }
        });
        if (TextUtils.isEmpty(itemId) || TextUtils.isEmpty(customerId)) {
            return;
        }
        wvCustomerDesc.loadUrl("file:///android_asset/secondaryPage06_CustomerProductDetails.html?itemId=" + itemId + "&customerId=" + customerId + "&" + GetDeviceId.id(this));
    }

    @OnClick(R.id.btn_back3)
    public void onClick() {
        processBackup();
    }

    private void processBackup() {
        //发送消息，组合列表重新请求数据
        EventBus.getDefault().post(new BooleanEvent(true, "customer"));
        //切换页面
        finish();
    }

    //埋点
    @Override
    protected void onResume() {
        super.onResume();
        TAnalytics.onPageStart("customerDetailPage");
    }

    @Override
    protected void onPause() {
        super.onPause();
        TAnalytics.onPageLoad("customerDetailPage", fromPage, "", "");
    }

    @Override
    public void onBackPressed() {
        processBackup();
    }
}
