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.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 implements AdViewSpreadListener {
	private AdViewSpreadManager adSpreadBIDView = null;
	private int count = 1;
	private int reqCounter = 0;
	public String[] permissions = null;
	private ViewGroup container;

	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.spread_layout);

		requestSpreadAd();
	}

	private void requestSpreadAd(){
		adSpreadBIDView = new AdViewSpreadManager(this,
				MainActivity.test_APPID,
				(RelativeLayout) findViewById(R.id.spreadlayout));

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

		adSpreadBIDView.setOnAdViewListener(this);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return false;
		return super.onKeyDown(keyCode, event);
	}
	@SuppressLint("ResourceType")
	@Override
	public void onAdNotifyCustomCallback( final int ruleTime,final  int delayTime) {
		final TextView tv1 = new TextView(this);
		final Button btn1 = new Button(this);
		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.setBackgroundColor(Color.TRANSPARENT);
		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);
				}
			}
		}, 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 ---------");

		this.finish();
	}

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

		this.finish();
	}
	@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 + "++++++++++++");
		}
		/*
		if (reqCounter++ < 3) {
			//reqCounter = 0;
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					requestSpreadAd();
				}
			}, 50);
		}else
		*/
		{
				//close it
//
//				Intent intent = new Intent();
//				intent.setClass(this, MainActivity.class);
//				startActivity(intent);

				this.finish();

		}
	}

	@Override
	public void onAdReceived() {

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

	@Override
	public void onAdSpreadPrepareClosed() {
//        Intent intent = new Intent();
//        intent.setClass(this, MainActivity.class);
//        startActivity(intent);
        this.finish();
		Log.i("AdViewBID", "[SpreadAPP] --------onAdSpreadPrepareClosed -----------");
	}

}
