package com.adviewbid.demo;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.kuaiyou.loader.AdViewBannerManager;
import com.kuaiyou.loader.loaderInterface.AdViewBannerListener;
import com.adviewbid.demo.R;


/**
 * banner
 * @author YYZ
 *
 */
public class AdBannerActivity extends Activity implements AdViewBannerListener,
		OnClickListener {
	private AdViewBannerManager adViewBIDView = null;
	private LinearLayout layout = null;

	private Button nextAd = null;
	private LinearLayout codeAd = null;
	private LinearLayout mixAd = null;
	private Button banner_back = null;
    private Button banner_rotate = null;

	private boolean isMREC = false;
	private boolean isVideo = false;

	private int fullHeight = 0;
	private void requestBanner() {

			adViewBIDView = null;
			int adSize = AdViewBannerManager.BANNER_AUTO_FILL;
			if (isMREC || isVideo) {
				adSize = AdViewBannerManager.BANNER_MREC;
			}else {
				adSize = AdViewBannerManager.BANNER_AUTO_FILL;
			}

			if (isVideo) {
				adViewBIDView = new AdViewBannerManager(AdBannerActivity.this,
							MainActivity.test_APPID,
							MainActivity.test_mrec_Video_POSID,
							adSize,
							true);
			}else {
				adViewBIDView = new AdViewBannerManager(AdBannerActivity.this,
							MainActivity.test_APPID,
							null,
							adSize,
							true);
			}

			adViewBIDView.setOnAdViewListener(this);
			adViewBIDView.setGDPR(true,
									"subject001",
									MainActivity.Gdpr,
									"p0001",
									"vendor001"
									);
			adViewBIDView.setRefreshTime(-1);


	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int banner_mode  = getIntent().getExtras().getInt("banner_mode", 0);
		if (banner_mode == 1)
			isMREC = true;
		else if (banner_mode == 2) {
			isVideo = true;
			isMREC = true;
		}

        if (isMREC || isVideo) {
            setContentView(R.layout.activity_banner_mrec);
		}
        else {
			setContentView(R.layout.activity_banner);
		}

		layout = (LinearLayout) findViewById(R.id.banner_layout);
		nextAd = (Button) findViewById(R.id.banner_change);
		codeAd = (LinearLayout) findViewById(R.id.banner_code);
		mixAd = (LinearLayout) findViewById(R.id.banner_mix);
		banner_back = (Button) findViewById(R.id.banner_back);
		banner_rotate = (Button) findViewById(R.id.banner_rotate);
		nextAd.setOnClickListener(this);
		banner_back.setOnClickListener(this);
		codeAd.setOnClickListener(this);
		mixAd.setOnClickListener(this);
		banner_rotate.setOnClickListener(this);
		//get layout height
		LinearLayout container = (LinearLayout) findViewById(R.id.banner_layout_root);
		LinearLayout.LayoutParams para = (LinearLayout.LayoutParams)container.getLayoutParams();
		fullHeight = para.height;
		if (isVideo)
		    banner_rotate.setVisibility(View.INVISIBLE);

		requestBanner();

		//adViewBIDView.logMode=false;
		adViewBIDView.setShowCloseBtn(true);
		adViewBIDView.setOpenAnim(true);

		if (null != layout)
			layout.addView(adViewBIDView.getAdViewLayout());
	}

	@Override
	protected void onResume() {
		super.onResume();
		//foreground-> background

	}
	@Override
	protected void onPause() {
		//foreground -> background
		super.onPause();
	}
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
            Log.i("AdBannerActivity", "--- now portrait ----------------");

        }
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            Log.i("AdBannerActivity", "--- now landscape ----------------");
        }
        requestBanner();
	}


	@Override
	public void onClick(View v) {
		if (null != adViewBIDView) {

			if (layout != null) {
				layout.removeView(adViewBIDView.getAdViewLayout());
			}

		}
		if (null == layout)
			return;
		layout.removeAllViews();


		switch (v.getId()) {
		case R.id.banner_mix:
			mixAd.setBackgroundResource(R.drawable.banner_selected);
			codeAd.setBackgroundResource(R.drawable.banner_normal);
			layout.addView(adViewBIDView.getAdViewLayout());
			break;

		case R.id.banner_code:
			Button button = (Button) findViewById(R.id.banner_back);
			LinearLayout titleLayout = (LinearLayout) findViewById(R.id.title_layout);
			codeAd.setBackgroundResource(R.drawable.banner_selected);
			mixAd.setBackgroundResource(R.drawable.banner_normal);
			LayoutParams layoutParams = new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			layoutParams.topMargin = titleLayout.getHeight()
					+ button.getHeight();
			layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
			this.addContentView(adViewBIDView.getAdViewLayout(), layoutParams);
			break;
		case R.id.banner_change:
            requestBanner();
			layout.addView(adViewBIDView.getAdViewLayout());
			break;
		case R.id.banner_back:
			this.finish();
			break;
		case R.id.banner_rotate:
		    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		    else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            layout.addView(adViewBIDView.getAdViewLayout());
            break;
		}


	}

	@Override
	public void onAdClicked() {
		Log.i("AdBannerActivity", "--- banner ---onAdClicked");
	}

	@Override
	public void onAdClosed() {
		Log.i("AdBannerActivity", "--- banner ---onAdClosedAd");
		if (null != adViewBIDView) {
			if (layout != null) {
				layout.removeView(adViewBIDView.getAdViewLayout());
			}
		}
//		if (null != adViewBIDView) {
//			ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
//			for (int i = 0; i < rootView.getChildCount(); i++) {
//				if (rootView.getChildAt(i) == adViewBIDView.getAdViewLayout()) {
//					rootView.removeView(adViewBIDView.getAdViewLayout());
//				}
//			}
//		}
//		if (null == layout)
//			return;
//		layout.removeAllViews();
	}

	@Override
	public void onAdDisplayed() {

		Log.i("AdBannerActivity", "--- banner --- onAdDisplayed  ----");
	}

	@Override
	public void onAdFailedReceived(String arg1) {
		Log.i("AdBannerActivity", "--- banner ---onAdRecieveFailed --" + arg1 + "---------");
	}

	@Override
	public void onAdReceived() {

		Log.i("AdBannerActivity", "--- banner ---onAdRecieved --------");
	}

	@Override
	public void onAdReady() {
		//if video mode can pass here
		Log.i("AdBannerActivity", "--- banner ---onAdReady --------");
		//adViewBIDView.playVideo(this);
	}
}
