package ai.pai.client.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.android.views.progressbar.ProgressWheel;

import ai.pai.client.R;
import ai.pai.client.utils.SystemUtils;

public class WebViewActivity extends BaseActivity implements View.OnClickListener {

    private ProgressWheel spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        SystemUtils.setImmerseLayout(this, findViewById(R.id.title_layout));

        spinner = (ProgressWheel)findViewById(R.id.wheel_wait_http);

        String url = getIntent().getStringExtra("url");
        String title = getIntent().getStringExtra("title");
        ((TextView) findViewById(R.id.title_bar_middle_txt)).setText(title);
        findViewById(R.id.title_bar_back_layout).setOnClickListener(this);
        findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);
        WebView webView = (WebView) findViewById(R.id.webview_contract);
        webView.getSettings().setJavaScriptEnabled(true);

        spinner.setVisibility(View.VISIBLE);
        spinner.startSpinning();

        spinner.postDelayed(new Runnable() {
            @Override
            public void run() {
                spinner.stopSpinning();
                spinner.setVisibility(View.GONE);
            }
        },2000);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
        webView.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if(newProgress>90){
                    spinner.stopSpinning();
                    spinner.setVisibility(View.GONE);
                }
            }
        });
        webView.loadUrl(url);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_bar_back_layout:
                finish();
                break;
            default:
                break;
        }
    }
}
