package com.adviewbid.demo;

import java.lang.reflect.Field;
import java.util.List;

import com.kuaiyou.loader.AdViewSpreadManager;
import com.kuaiyou.loader.InitSDKManager;
import com.kuaiyou.loader.loaderInterface.AdViewSpreadListener;
import com.adviewbid.demo.R;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

/**
 * spread ads
 * @author YYZ
 *
 */
public class AdSpreadActivity extends Activity  {
	private AdViewSpreadManager adSpreadBIDView = null;
	private int count = 1;
	private AdViewSpreadListener advListener;
	private Context mCtx = null;

	private void requestSpreadAd(){
		adSpreadBIDView = new AdViewSpreadManager(
				//this,
				getApplication().getApplicationContext(),
				MainActivity.offical_APPID,
				MainActivity.offical_spread_POSID,
				(RelativeLayout) findViewById(R.id.spreadlayout));

		adSpreadBIDView.setLogo(R.drawable.logo);
		adSpreadBIDView.setBackgroundColor(Color.WHITE);
		adSpreadBIDView.setSpreadNotifyType(AdViewSpreadManager.NOTIFY_COUNTER_CUSTOM);

		adSpreadBIDView.setOnAdViewListener(advListener);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		mCtx = this;

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.spread_layout);

		advListener = new com.kuaiyou.loader.loaderInterface.AdViewSpreadListener() {
			@SuppressLint("ResourceType")
			@Override
			public void onAdNotifyCustomCallback( final int ruleTime,final  int delayTime) {
				final TextView tv1 = new TextView(mCtx);
				final Button btn1 = new Button(mCtx);
				final LayoutParams btnLp = new LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				final LayoutParams tvLp = new LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);

				tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
				btn1.setId(123123);
				tv1.setBackgroundColor(Color.WHITE);
				btn1.setText("Skip");
				tv1.setText(ruleTime + delayTime + "");

				btnLp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				tvLp.addRule(RelativeLayout.LEFT_OF, btn1.getId());

				adSpreadBIDView.getParentLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						btn1.setVisibility(View.VISIBLE);
						btn1.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								adSpreadBIDView.cancelAd();
							}
						});
					}
				}, ruleTime * 1000);

				adSpreadBIDView.getParentLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						if (ruleTime + delayTime - count >= 1) {
							tv1.setBackgroundColor(Color.WHITE);
							tv1.setText(ruleTime + delayTime - count + "");
							count++;
							adSpreadBIDView.getParentLayout().postDelayed(this, 1000);
						}else{
							//time is up
							Log.i("AdViewBID", "[SpreadAPP] --------time is up !!!! ---------");

							//((Activity)getBaseContext()).finish();
						}
					}
				}, 1000);

				adSpreadBIDView.getParentLayout().addView(btn1, btnLp);
				adSpreadBIDView.getParentLayout().addView(tv1, tvLp);
				btn1.setVisibility(View.INVISIBLE);
			}

			@Override
			public void onAdClicked() {
				Log.i("AdViewBID", "[SpreadAPP] --------onAdClicked ---------");
			}

			@Override
			public void onAdClosed() {
				Log.i("AdViewBID", "[SpreadAPP] --------onAdClosedAd ---------");
						onClose();
			}

			@Override
			public void onAdClosedByUser() {
				Log.i("AdViewBID", "[SpreadAPP] -------- onAdClosedByUser -----------");

						onClose();
			}
			@Override
			public void onAdDisplayed() {

				Log.i("AdViewBID", "[SpreadAPP] --------onAdDisplayed--------------");
			}
			@Override
			public void onAdFailedReceived(String arg1) {
				Log.i("AdSpreadActivity", "+++++++++++ onAdRecieveFailed: " + arg1 + "++++++++++");
				/* wilder 2019 for test */
				if (arg1.contains("custom://")) {
					Log.i("AdSpreadActivity", "+++++++++++++++ :" + arg1 + "++++++++++++");
				}
						onClose();
					}
			@Override
			public void onAdReceived() {

				Log.i("AdViewBID", "[SpreadAPP] --------onAdRecieved -----------");
			}

			@Override
			public void onAdSpreadPrepareClosed() {
				onClose();
				Log.i("AdViewBID", "[SpreadAPP] --------onAdSpreadPrepareClosed -----------");
			}

		};

		requestSpreadAd();

	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return false;
		return super.onKeyDown(keyCode, event);
	}

	private void onClose() {
		this.finish();
	}

	

}
