package com.tofly.ghrq_route.common.ui.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.service.NetStateService;
import com.tofly.ghrq_route.common.util.net.NetUtil;

/**
 * Created by zombiexq on 2016/6/27.
 */
public class UiWeb extends BaseUi {

    private WebView webview;
    private TextView prompt;
    boolean mBound = false;
    NetStateService netStateService;
    private String url;
    private String title;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_webview);

        initView();
        initData();
        initListener();

    }

    private void initView(){
        webview = (WebView) findViewById(R.id.webview);
        prompt = (TextView) findViewById(R.id.webview_prompt);
    }
    private void initData(){

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        url  = bundle.getString("url");
        title = bundle.getString("title");
        if(NetUtil.getNetworkState(UiWeb.this)!=NetUtil.NETWORN_NONE){
            prompt.setVisibility(View.INVISIBLE);
            webview.setVisibility(View.VISIBLE);
            webview.loadUrl(url);
            //启用支持javascript
            WebSettings settings = webview .getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            webview.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                    view.loadUrl(url);
                    return true;
                }
            });
        }else{
            prompt.setVisibility(View.VISIBLE);
            webview.setVisibility(View.INVISIBLE);
        }

        bind();

    }

    private void initListener(){
        /*禁止Webview的长按事件，否则会覆盖Toolbar*/
        webview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
    }

    private void bind() {
        Intent intent = new Intent(UiWeb.this, NetStateService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
    private void unbind() {
        if (mBound) {
            unbindService(serviceConnection);
            mBound = false;
        }
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBound = false;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            netStateService = ((NetStateService.MyBinder) service)
                    .getService();
            mBound = true;
            netStateService.setOnGetConnectState(new NetStateService.GetConnectState() { // 添加接口实例获取连接状态
                @Override
                public void GetState(boolean isConnected) {
                    if (isConnected) {
                        handler.sendEmptyMessage(1);

                    } else {
                        handler.sendEmptyMessage(2);

                    }

                }
            });
        }
    };


    Handler handler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:// 已连接
                    //      Toast.makeText(UiWebView.this, "网络已经连接", Toast.LENGTH_LONG).show();
                    prompt.setVisibility(View.INVISIBLE);
                    webview.setVisibility(View.VISIBLE);
                    webview.loadUrl(url);
                    //启用支持javascript
                    WebSettings settings = webview .getSettings();
                    settings.setJavaScriptEnabled(true);
                    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
                    webview.setWebViewClient(new WebViewClient() {
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                            view.loadUrl(url);
                            return true;
                        }
                    });


                    break;
                case 2:// 未连接
                    //        Toast.makeText(UiWebView.this, "网络未连接" ,Toast.LENGTH_LONG).show();
                    prompt.setVisibility(View.VISIBLE);
                    webview.setVisibility(View.INVISIBLE);
                    break;
                default:
                    break;
            }
            ;
        };

    };

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unbind();
    }

}
