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.FrameLayout;
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 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 String posID = "";
	private String appID = "";
	private boolean isMREC = false;
	private boolean isVideo = false;

	private AdViewBannerListener advListener;
	private int fullHeight = 0;
	private boolean showOhide = false;

	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) {
					appID = MainActivity.offical_APPID;
					posID = MainActivity.offical_mrec_video_POSID;
				}else {
					appID = MainActivity.offical_APPID;
					if (isMREC)
						posID = MainActivity.offical_mrec_posid;
					else
						posID = MainActivity.offical_banner_POSID;
				}
			adViewBIDView = new AdViewBannerManager(
					this,
					//getApplication().getApplicationContext(),
					appID,
					posID,
					adSize,
					false);

			adViewBIDView.setGDPR(true,
							"1",
									MainActivity.Gdpr,
							"p0001",
							"vendor001"
							);

			adViewBIDView.setGDPR2(0,
					MainActivity.Gdpr
			);

			adViewBIDView.setRefreshTime(20);
			if (isVideo)
				adViewBIDView.setVideoMode(true);

			adViewBIDView.setOnAdViewListener(advListener);
	}

	@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);
			LinearLayout videoContainer = (LinearLayout) findViewById(R.id.banner_layout);
			FrameLayout.LayoutParams paramsV = (FrameLayout.LayoutParams)videoContainer.getLayoutParams();
			paramsV.width = -1; //fill_parent
			videoContainer.setLayoutParams(paramsV);
		}

		advListener = new com.kuaiyou.loader.loaderInterface.AdViewBannerListener() {
			@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());
					}
				}
			}
			@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 --------");
			}
		};

		requestBanner();

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

		if (null != layout) {
			showOhide = true;
			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);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.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());
			if (showOhide == true) {
				showOhide = false;
				layout.removeView(adViewBIDView.getAdViewLayout());
			}else {
				showOhide = true;
				layout.addView(adViewBIDView.getAdViewLayout());
			}

			break;
		}


	}


}
