package com.taikor.investment.modules.group;

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.event.BooleanEvent;
import com.taikor.investment.mylibrary.TAnalytics;
import com.taikor.investment.utils.GetDeviceId;

import org.greenrobot.eventbus.EventBus;

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

/**
 * 组合详情
 * Created by Any on 2017/5/4.
 */
public class GroupDescActivity extends BaseActivity {

    @BindView(R.id.wv_group_desc)
    WebView wvGroupDesc;

    private String itemId, fromPage;
    private ProgressDialog progressDialog;
    private static final String TAG = "GroupDescActivity";

    @Override
    public int getContentViewId() {
        return R.layout.activity_group_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() {
        //支持App内部JavaScript交互
        WebSettings settings = wvGroupDesc.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);

        wvGroupDesc.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;
        }
        wvGroupDesc.loadUrl("file:///android_asset/secondaryPage_ProductDetails.html?itemId=" + itemId + "&" + GetDeviceId.id(this));
    }

    private void processBackup() {
        switch (fromPage) {
            case "officialPage"://返回上一个页面
            case "masterPage":
            case "cardPage":
            case "groupPage":
                wvGroupDesc.goBack();
                break;
            case "breakPage"://返回组合列表
            case "flexiblePage":
            case "selfDefinePage":
                //发送消息，组合列表重新请求数据
                EventBus.getDefault().post(new BooleanEvent(true, "group"));
                break;
        }
        TAnalytics.onClick("quitGroupDetail", itemId); //埋点
        GroupDescActivity.this.finish();
    }

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

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

    //处理返回键
    @Override
    public void onBackPressed() {
        processBackup();
    }
    
    @OnClick(R.id.btn_back2)
    public void onClick() {
        processBackup();
    }
}
