package com.adviewbid.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.advg.loader.AdViewVideoManager;
import com.advg.loader.loaderInterface.AdViewVideoListener;
import com.adviewbid.demo.R;

import java.io.Serializable;

/**
 * video
 * @author YYZ
 *
 */
public class AdVideoActivity extends Activity implements OnClickListener {

	private Button nextVideo = null;
	private Button video_back = null;
	private AdViewVideoManager videoManager = null;
	private AdViewVideoListener advListener;

	public final static String TAG = "AdViewVideoBID";

	private void requestAd() {
		videoManager = new AdViewVideoManager(this,
				MainActivity.offical_APPID,
                MainActivity.offical_Video_POSID,
				advListener,
				false);

		// screen orientation , pls refer to ActivityInfo.SCREEN_XXXXXX definition
		videoManager.setVideoOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video);
		nextVideo = (Button) findViewById(R.id.video_change);
		video_back = (Button) findViewById(R.id.video_back);
		nextVideo.setOnClickListener(this);
		video_back.setOnClickListener(this);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏

		advListener = new com.advg.loader.loaderInterface.AdViewVideoListener() {
			@Override
			public void onFailedReceivedVideo(String error) {
				Log.i(TAG, "onFailedRecievedVideo:"+error);
				popupInfo(error);
//		nextVideo.setEnabled(true);
			}
			@Override
			public void onVideoStartPlayed() {

				Log.i(TAG, "++++++++++++ [app]onVideoStartPlayed ++++++++++");
			}
			@Override
			public void onVideoFinished() {
				Log.i(TAG, "++++++++++++[app] onVideoFinished ++++++++++++");
			}
			@Override
			public void onVideoClosed() {
				Log.i(TAG, "++++++++++++[app] onVideoClosed ++++++++++++");

				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
			}
			@Override
			public void onPlayedError(String arg0) {
				Log.i(TAG, "onPlayedError:"+arg0);
			}
			@Override
			public void onReceivedVideo(String arg0) {

				Log.i(TAG, "onRecievedVideo:"+arg0);
			}
			@Override
			public void onVideoReady() {
				Log.i(TAG, "onVideoReady");
				//videoManager.playVideo(this);
			}
			@Override
			public void onVideoClicked(int downX, int downY) { //wilder 2021 click event
				Log.i(TAG, "[AdVideoActivity]--- onVideoClicked ----");
			}
		};


		requestAd();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.video_back:
			this.finish();
			break;
		case R.id.video_change:
			Log.i(TAG, "clickButton");
			requestAd();
			break;
		}

	}

	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	//wilder 20210818
	private void popupInfo(String code) {
		AlertDialog.Builder builder;
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
		} else {
			builder = new AlertDialog.Builder(this);
		}
		builder.setTitle("Failed to load adview banner ad")
				.setMessage("Error code: \n" + code)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.show();
	}

}
