package com.taikor.investment.modules.product;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
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.mylibrary.TAnalytics;
import com.taikor.investment.utils.GetDeviceId;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 产品详情
 * Created by Any on 2017/5/4.
 */
public class ProductDescActivity extends BaseActivity {
    
    @BindView(R.id.wv_product_desc)
    WebView wvProductDesc;

    private String itemId, fromPage;
    private ProgressDialog progressDialog;

    @Override
    public int getContentViewId() {
        return R.layout.activity_product_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");
        fromPage = intent.getStringExtra("from");
    }

    @Override
    public void initData() {
        WebSettings settings = wvProductDesc.getSettings();
        //支持App内部JavaScript交互
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        wvProductDesc.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)) {
            return;
        }
        wvProductDesc.loadUrl("file:///android_asset/informationPage01_FundDetails.html?itemId=" + itemId + "&" + GetDeviceId.id(this));
    }
    
    //埋点
    @Override
    protected void onResume() {
        super.onResume();
        TAnalytics.onPageStart("productDetailPage");
    }

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

    @OnClick(R.id.btn_back)
    public void onClick() {
        //如果不能返回上一个页面，自己处理
        if (wvProductDesc.canGoBack()) {
            wvProductDesc.goBack();//返回上一个页面
        } else {
            //埋点
            TAnalytics.onClick("quitProductDetail", itemId);
            finish();
        }
    }
}
