package com.yidianling.ydlcommon.custom.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ProgressBar;

import com.tencent.smtt.sdk.WebView;
import com.yidianling.common.tools.LogUtil;
import com.yidianling.ydlcommon.R;

/**
 * Created by jinkai on 16/5/9.
 */
public class ProgressWebView extends WebView {
    protected ProgressBar progressbar;

    protected String Title;

    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);

        progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        progressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5));
        progressbar.setProgressDrawable(getResources().getDrawable(R.drawable.web_progressbar));
        addView(progressbar);
        setProgressbar(progressbar);
        setWebChromeClient(new WebChromeClient());
    }

    public class WebChromeClient extends com.tencent.smtt.sdk.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            Title = title;
        }


    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public ProgressBar getProgressbar() {
        return progressbar;
    }

    public void setProgressbar(ProgressBar progressbar) {
        this.progressbar = progressbar;
    }
}
