package com.danding.yang800;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.danding.yang800.Presenter.GiftInfoActivityPresenter;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StatusColorUtil;
import com.yanzhenjie.sofia.Sofia;

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

public class GiftInfoActivity extends BaseActivity {
    @BindView(R.id.giftInfo_goodsIv)
    ImageView giftInfo_goodsIv;
    @BindView(R.id.giftInfo_webview)
    WebView giftInfo_webview;
    @BindView(R.id.giftInfo_backTv)
    TextView giftInfo_backTv;
    @BindView(R.id.giftInfo_buyTv)
    TextView giftInfo_buyTv;
    GiftInfoActivityPresenter presenter;
    @BindView(R.id.giftInfo_goodsNameTv)
    TextView giftInfo_goodsNameTv;
    String storeSkuSn = "";
    @BindView(R.id.goodsInfo_priceIntTv)
    TextView goodsInfo_priceIntTv;
    @BindView(R.id.goodsInfo_priceTv)
    TextView goodsInfo_priceTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_gift_info);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        Intent intent = getIntent();
        storeSkuSn = intent.getStringExtra("storeSkuSn");
        presenter = new GiftInfoActivityPresenter(this);
        presenter.getInfo(storeSkuSn);
//        Glide.with(this).load("http://dante-img.oss-cn-hangzhou.aliyuncs.com/online/90397651ea161ed8dea955780d223ede.jpg").into(giftInfo_goodsIv);
        initWeb();
//        StringBuilder sb = new StringBuilder();
//        sb.append("<style>");
//        sb.append("img { width:100%;}");
//        sb.append("</style>");
//        String goodsContent = "<img src='http://dante-img.oss-cn-hangzhou.aliyuncs.com/online/297253996d5b67b34b14d0105046acbc.jpg'>";
//        sb.append(goodsContent);
//        giftInfo_webview.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8",
//                null);
    }

    private void initWeb() {
        WebSettings wSet = giftInfo_webview.getSettings();
        wSet.setJavaScriptEnabled(true);
        giftInfo_webview.clearCache(true);
        giftInfo_webview.setWebChromeClient(new WebChromeClient() {
        });
        wSet.setAllowFileAccess(true);
        wSet.setCacheMode(WebSettings.LOAD_NO_CACHE);
        wSet.setJavaScriptEnabled(true);
        wSet.setUseWideViewPort(true);
        wSet.setLoadWithOverviewMode(true);

    }

    @OnClick({R.id.giftInfo_backTv, R.id.giftInfo_buyTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.giftInfo_backTv:
                finish();
                break;
            case R.id.giftInfo_buyTv:
                Intent intent = new Intent(GiftInfoActivity.this, OrderPayActivity.class);
                intent.putExtra("storeSkuSn", storeSkuSn);
                startActivity(intent);
                break;
        }
    }

    public void initData(String skuName, String imageUrl, String content, String priceInt, String price) {
        giftInfo_goodsNameTv.setText(skuName);
        goodsInfo_priceIntTv.setText(priceInt);
        goodsInfo_priceTv.setText(price);
        sp.edit().putString("priceInt", priceInt).commit();
        sp.edit().putString("price", price).commit();
        Glide.with(this).load(imageUrl).into(giftInfo_goodsIv);
        StringBuilder sb = new StringBuilder();
        sb.append("<style>");
        sb.append("img { width:100%;}");
        sb.append("p { -webkit-margin-before:0em; -webkit-margin-after:0em }");
        sb.append("</style>");
        sb.append(content);
        giftInfo_webview.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8",
                null);
    }
}
