package com.lianxin.pubqq.main;

import java.lang.reflect.InvocationTargetException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lianxin.pubqq.main.MainActivity;
import com.lianxin.pubqq.R;

//播放网络视频（没有细化）
public class Fragment_Movie extends Fragment {
	private Activity ctx;
	private View layout, layout_head;;
	public RelativeLayout errorItem;

	private MainActivity parentActivity;
	private ImageView img_back;
	private TextView txt_title;
	private WebView mWebView;
	private ProgressBar progressbar;
	private String strurl = "";
	private MyTimer mTimer;
	private int progress = 0;	
	  private MotionEvent mCurrentDownEvent;  
	    private MotionEvent mPreviousUpEvent;
	    private static final int DOUBLE_CLICK_TIME = 350;
	    private final int DOUBLE_TAP_TIMEOUT = 200;	    
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (layout == null) {
			ctx = this.getActivity();
			parentActivity = (MainActivity) getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.fragment_movie,
					null);

			initControl();			
			initData();			
			initView();			
			setOnListener();
		} else {
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		return layout;
	}

	protected void initControl() {

		mWebView = (WebView) layout.findViewById(R.id.mwebview);
		progressbar = (ProgressBar) layout.findViewById(R.id.progressbar);
	}	
	
	@Override
	public void onResume() {
		super.onResume();
		try {
			mWebView.getClass().getMethod("onResume")
					.invoke(mWebView, (Object[]) null);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onPause() {
		super.onPause();
		try {
			mWebView.getClass().getMethod("onPause")
					.invoke(mWebView, (Object[]) null);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}

	@SuppressLint("SetJavaScriptEnabled")
	protected void initView() {

		strurl = "http://www.iqiyi.com/";
		if (!TextUtils.isEmpty(strurl)) {
			mWebView.getSettings().setJavaScriptEnabled(true);
			mWebView.setWebViewClient(new WeiboWebViewClient());
			mWebView.setWebChromeClient(new WebChromeClient());
			mWebView.loadUrl(strurl);
		}
	}

	private class WeiboWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return super.shouldOverrideUrlLoading(view, url);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			if (mTimer == null) {
				mTimer = new MyTimer(15000, 50);
			}
			mTimer.start();
			progressbar.setVisibility(View.VISIBLE);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			mTimer.cancel();
			progress = 0;
			progressbar.setProgress(100);
			progressbar.setVisibility(View.GONE);
		}
	}

	//@Override
	protected void initData() {

	}

	private void setOnListener() {
	
		OnTouchListener listener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (mPreviousUpEvent != null
							&& mCurrentDownEvent != null
							&& isConsideredDoubleTap(mCurrentDownEvent,
									mPreviousUpEvent, event)) {					
						((MainActivity) getActivity()).setLayBottomVisible();						
					}
					mCurrentDownEvent = MotionEvent.obtain(event);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					mPreviousUpEvent = MotionEvent.obtain(event);
				}
				return false;

			}
		};
		mWebView.setOnTouchListener(listener);			
	}
	
	private boolean isConsideredDoubleTap(MotionEvent firstDown,  
            MotionEvent firstUp, MotionEvent secondDown) {  
        if (secondDown.getEventTime() - firstUp.getEventTime() > DOUBLE_TAP_TIMEOUT) {  
            return false;  
        }  
        int deltaX = (int) firstUp.getX() - (int) secondDown.getX();  
        int deltaY = (int) firstUp.getY() - (int) secondDown.getY();  
        return deltaX * deltaX + deltaY * deltaY < 10000;  
    }	
	
	/* 定义一个倒计时的内部类 */
	private class MyTimer extends CountDownTimer {
		public MyTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			progress = 100;
			progressbar.setVisibility(View.GONE);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			if (progress == 100) {
				progressbar.setVisibility(View.GONE);
			} else {
				progressbar.setProgress(progress++);
			}
		}
	}


}
