package cn.net.communal.cpzshandset.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import cn.net.communal.cpzshandset.R;
import cn.net.communal.cpzshandset.common.config.TConfig;
import cn.net.communal.cpzshandset.common.util.LogUtil;
import cn.net.communal.cpzshandset.common.util.StringUtils;
import cn.net.communal.cpzshandset.service.presenter.LoginConstact;
import cn.net.communal.cpzshandset.service.presenter.LoginConstact.Presenter;
import cn.net.communal.cpzshandset.ui.activity.base.BaseActivity;
import cn.net.communal.cpzshandset.ui.weight.MyToast;

public class LoginActivity extends BaseActivity implements LoginConstact.View, OnClickListener{

	private LoginConstact.Presenter presenter;
	private TextView tv_update_config_info,tv_update_date_tips_info;
	private EditText et_login_account,et_login_pwd;
	private View btn_login_submit;
	private Context context;
	private MyToast myToast;
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
	}
	@Override
	public int loadView() {
		context = this;
		myToast = new MyToast(context);
		return R.layout.activity_login;
	}

	@Override
	public void initTitleView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void initView() {
		tv_update_config_info = (TextView) findViewById(R.id.tv_update_config_info);		
		et_login_account = (EditText) findViewById(R.id.et_login_account);
		et_login_pwd = (EditText) findViewById(R.id.et_login_pwd);
		btn_login_submit = findViewById(R.id.btn_login_submit);
		tv_update_date_tips_info = (TextView) findViewById(R.id.tv_update_date_tips_info);
	}

	@Override
	public void initPresenter() {
		presenter = createService.createLoginPresenter(cacheG, context, this, getIntent());		
	}

	@Override
	public void setListener() {
		tv_update_config_info.setOnClickListener(this);
		btn_login_submit.setOnClickListener(this);
	}

	@Override
	public void initData() {
		
	}

	@Override
	public void setPresenter(Presenter presenter) {
		if (presenter != null) {
			this.presenter = presenter;
		}
	}

	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showMsgToast(String msg, int duration) {
		if(myToast == null || StringUtils.isEmpty(msg)) return;
		myToast.show(msg, duration);
	}

	@Override
	public void finishPage() {
		finish();
	}

	@Override
	public void setUpdateBtnVisibility(int visibility) {
		if(tv_update_config_info == null) return;
		tv_update_config_info.setVisibility(visibility);		
	}

	@Override
	public void setBottomTipsVisibility(int visibility) {
		if(tv_update_date_tips_info == null) return;
		tv_update_date_tips_info.setVisibility(visibility);
	}

	@Override
	public void setUpdateBtnStr(String btnUpdateStr) {
		if(tv_update_config_info == null || btnUpdateStr == null) return;
		tv_update_config_info.setText(btnUpdateStr+"");
	}

	@Override
	public void setBottomTipsStr(String bottomTips) {
		if(tv_update_date_tips_info == null || bottomTips == null) return;
		tv_update_date_tips_info.setText(bottomTips+"");
	}

	@Override
	public String getAccountStr() {
		return et_login_account != null ? et_login_account.getText().toString().trim():"";
	}

	@Override
	public void setAccountStr(String account) {
		if(et_login_account == null || account == null) return;
		et_login_account.setText(account);
	}

	@Override
	public String getPwdStr() {
		return et_login_pwd != null ? et_login_pwd.getText().toString().trim() : "";
	}

	@Override
	public void setPwdStr(String pwd) {
		if(et_login_pwd == null || pwd == null) return;
		et_login_pwd.setText(pwd+"");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_update_config_info:
			if (presenter != null) {
				presenter.updateBtnClick();
			}
			break;

		case R.id.btn_login_submit:
			if (presenter != null) {
				presenter.loginClick();
			}
			break;
		}
	}

}
