package com.dbs.mplus.fatima.framework.view.popup;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.dbs.mplus.fatima.framework.util.MPlusDeviceUtil;
import com.dbs.mplus.fatima.framework.util.MPlusLogUtil;
import com.dbs.mplus.fatima.framework.util.MPlusResourceUtil;

/**
 * push가 오면 메시지를 보여줄 화면
 * @author sungchul
 *
 */
public abstract class MPlusNotifyActivity extends Activity {

	/**
	 * 생성자
	 */
	public MPlusNotifyActivity() {

	}

	/**
	 * 아이콘에 표시될 리소스
	 * @param imageResourceId
	 */
	public void setIconImage(int imageResourceId) {
		int id = MPlusResourceUtil.getIdByName(this, "mfNotifyPopupImageView");
		final ImageView iv = (ImageView) this.findViewById(id);
		iv.setImageResource(imageResourceId);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(MPlusResourceUtil.getLayoutIdByName(this, "mf_activity_notify_popup"));

		MPlusLogUtil.DEBUG(this.getClass(), "onCreate... ");
//        MPlusLogUtil.DEBUG(this.getClass(), "task info : " + MPlusServiceUtil.getActivityStackList(this.getApplicationContext()));

		// title
		TextView titleTV = (TextView) this.findViewById(MPlusResourceUtil.getIdByName(this, "mfNotifyPopupTitle"));
		titleTV.setText(MPlusResourceUtil.getStringByName(this, "app_name"));

		// 수신메시지 검사
		final Intent intent = this.getIntent();
		if(intent == null) {
			this.finish();
			return;
		}

		// 화면을 켜 줄 가치가 있을때만 설정
		this.getWindow().addFlags(
				// 화면 켜기
				WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON /* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */
	            // 화면이 잠겨있을 때 보여주기
				| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
	            // 키잠금 해제
	            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

//		MPlusLogUtil.DEBUG(this.getClass(), "현재상태 : " + bScreenOn);



		// 화면 남은거 초기화
		this.beforeProcessAction();


		// 수신받은 메시지 처리
		this.processAction(intent);
	}


	@Override // 창이 떠 있는 상태에서 새로운 intent가 들어온 경우
	protected void onNewIntent (Intent intent) {
		super.onNewIntent(intent);
		//
		//MPlusLogUtil.DEBUG(this.getClass(), "onNewIntent : " + MPlusIntentUtil.getIntentString(intent));
		// 수신 받은 메시지를 처리하면 됨
		this.processAction(intent);
	}

	/**
	 * onCreate와 processAction 사이에 호출
	 *
	 */
	protected abstract void beforeProcessAction();

	/**
	 * 수신받은 메시지 처리
	 * @param intent
	 */
	protected abstract void processAction(Intent intent);

	/**
	 * 긍정버튼
	 * @return
	 */
	protected Button getYesButton() {
		int id = MPlusResourceUtil.getIdByName(this, "mfNotifyPopupYesBtn");
		return (Button) this.findViewById(id);
	}

	/**
	 * 부정버튼
	 * @return
	 */
	protected Button getNoButton() {
		int id = MPlusResourceUtil.getIdByName(this, "mfNotifyPopupNoBtn");
		return (Button) this.findViewById(id);
	}

	/**
	 * 텍스트 영역
	 * @return
	 */
	protected TextView getTextView() {
		int id = MPlusResourceUtil.getIdByName(this, "mfNotifyPopupTv");
		return (TextView) this.findViewById(id);
	}
	/**
	 * 긍정 버튼만 보이게
	 */
	protected void setViewOnlyYesButton() {
		Button button = null;

		// 보기버튼은 중간에
		button = this.getYesButton();
		final LayoutParams layout = (LayoutParams) button.getLayoutParams();
		layout.width = LayoutParams.WRAP_CONTENT;
		layout.gravity = Gravity.CENTER_HORIZONTAL;
		layout.leftMargin = 0;
		layout.rightMargin = 0;
		button.setLayoutParams(layout);

		// 닫기버튼 없애기
		button = this.getNoButton();
		button.setVisibility(View.GONE);
	}
	/**
	 * 부정 버튼만 보이게
	 */
	protected void setViewOnlyNoButton() {
		Button button = null;

		// 보기버튼 없애기
		button = this.getYesButton();
		button.setVisibility(View.GONE);

		// 닫기버튼 중간에
		button = this.getNoButton();
		final LayoutParams layout = (LayoutParams) button.getLayoutParams();
		layout.width = LayoutParams.WRAP_CONTENT;
		layout.gravity = Gravity.CENTER_HORIZONTAL;
		layout.leftMargin = 0;
		layout.rightMargin = 0;
		button.setLayoutParams(layout);
	}
}
