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

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.HomeConstact;
import cn.net.caoyao.cpzshandset.service.presenter.HomeConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class HomeActivity extends BaseActivity implements HomeConstact.View {
	private HomeConstact.Presenter presenter;
	// private CacheGet cacheG;
	private Context context;
	private TextView tv_title_common;
	private GridView gv_main;
	private View btn_logout;
	private TextView tv_companyName;
	private MyToast myToast;
	private long startTimeMillis;

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		if (presenter != null) {
			presenter.onNewIntent(intent);
		}

	}

	@Override
	public int loadView() {
		init();
		return R.layout.activity_home;
	}

	private void init() {
		// cacheG = new CacheGet();
	
		context = this;
		myToast = new MyToast(context);
	}

	@Override
	public void initTitleView() {
		tv_title_common = (TextView) findViewById(R.id.tv_title_common);
		tv_title_common.setText(getResources().getString(R.string.home_title_str));
	}

	@Override
	public void initView() {
		gv_main = (GridView) findViewById(R.id.gv_main);
		btn_logout = findViewById(R.id.btn_logout);
		tv_companyName = findViewById(R.id.tv_companyName);

	}

	@Override
	protected void onResume() {
		super.onResume();
		SharedPreferences user_token =getSharedPreferences("data", 0);
		String companyName = user_token.getString("companyName", "");
		tv_companyName.setText(companyName);
	}

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

	@Override
	public void setListener() {
		if (gv_main == null)
			return;
		gv_main.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (presenter != null) {
					presenter.onItemClickListener(position);
				}
			}
		});
		btn_logout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (presenter != null) {
					presenter.logoutClick();
				}
			}
		});
	}

	@Override
	public void initData() {

	}

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

	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		if (intent == null)
			return;
		startActivityForResult(intent, reqCode);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (presenter != null) {
			presenter.onActivityResult(requestCode, resultCode, data);
		}
	}

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

	@Override
	public void setAdapter(BaseAdapter adapter) {
		if (gv_main == null || adapter == null)
			return;
		gv_main.setAdapter(adapter);
	}

	@Override
	public void setHomeTitle(String homeTitle) {
		if (tv_title_common == null || StringUtils.isEmpty(homeTitle))
			return;
		tv_title_common.setText(homeTitle + "");

	}

	@Override
	public void onBackPressed() {
		long endTimeMillis = System.currentTimeMillis();
		if (endTimeMillis - startTimeMillis < 2000) {
			exitApplication();
		} else {
			myToast.show(getResources().getString(R.string.exit_application_hint), Toast.LENGTH_SHORT);
			startTimeMillis = endTimeMillis;
		}
	}

	@Override
	public void setLogoutVisibility(int visibility) {
		if (btn_logout == null)

			return;
		btn_logout.setVisibility(visibility);
	}

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

}
