package com.itant.joycity.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.itant.joycity.R;
import com.itant.joycity.bean.OfficialItem;

/**
 * Created by Jason on 2016/10/7.
 */
public class OfficialDetailActivity extends BaseActivity {
    private OfficialItem article;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar("详情");
        setBackable(true);


        article = (OfficialItem) getIntent().getSerializableExtra("article");
        if (article != null) {

            setTitle(article.getTitle());




            String head = "<html>\n" +
                    "<head>\n" +
                    "<meta name=\"viewport\" content=\"initial-scale=1, user-scalable=0, minimal-ui\" charset=\"UTF-8\">\n";


            String css = "<style type=\"text/css\">img{\n" +
                    " max-width:100%;\n" +
                    " height:auto;\n" +
                    "}</style>";

            String head2 = "</head>\n" +
                    "<body>";

            String tail = "</body>\n" +
                    "</html>";

            String html = head + css + head2 + article.getContent() + tail;

            WebView wv_content = (WebView) findViewById(R.id.wv_content);
            WebSettings settings = wv_content.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setBuiltInZoomControls(false);// 不可缩放
            settings.setUseWideViewPort(true);// 设置显示完整网页
            settings.setLoadWithOverviewMode(true);
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            wv_content.setBackgroundColor(0x00000000);

            wv_content.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
                    super.onShowCustomView(view, callback);
                }
            });
            wv_content.setDownloadListener(new MyWebViewDownLoadListener());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
            wv_content.loadData(html, "text/html; charset=UTF-8", null);
        }
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_official_detail;
    }


    private class MyWebViewDownLoadListener implements DownloadListener {

        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }

    }
}
