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

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
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.Button;
import android.widget.ListView;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.SettingConstact;
import cn.net.caoyao.cpzshandset.service.presenter.SettingConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class SettingActivity extends BaseActivity implements SettingConstact.View {
	private TextView tv_settion_version;
	private ListView lv_setting;
	private SettingConstact.Presenter presenter;
	private MyToast myToast;
	private Context context;
	// private int testData = -1;

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

	private void init() {
		context = this;
		myToast = new MyToast(context);
		// Intent intent = getIntent();
		// testData = intent.getIntExtra("testData", -1);
		// LogUtil.e(TConfig.Echo, "testData:" + testData);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		// LogUtil.d(TConfig.Echo, "onNewIntent intent:" + testData);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		myToast.cancel();
	}

	@Override
	public void initTitleView() {
		View image_title_back_common = findViewById(R.id.image_title_back_common);
		image_title_back_common.setVisibility(View.VISIBLE);
		image_title_back_common.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		TextView tv_title_common = (TextView) findViewById(R.id.tv_title_common);
		Button btn_unlogin = (Button) findViewById(R.id.btn_unlogin);
		Button btn_net_test = (Button) findViewById(R.id.btn_net_test);
		tv_title_common.setText("设置");
		btn_unlogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SharedPreferences sp=getSharedPreferences("data",0);
				SharedPreferences.Editor edit = sp.edit();
				edit.putString("appToken","");
				edit.commit();
				Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
				startActivity(intent);
			}
		});
		btn_net_test.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingActivity.this, JianCeActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public void initView() {
		tv_settion_version = (TextView) findViewById(R.id.tv_settion_version);
		lv_setting = (ListView) findViewById(R.id.lv_setting);
		String versionStr = getVersion();
		if (Config.isDebuger) {
			// tv_settion_version.setText("测试版本 V"+getVersion());
			versionStr = "测试版本 V" + versionStr;
		} else {
			// tv_settion_version.setText("版本 V"+getVersion());
			versionStr = "版本 V" + versionStr;
		}
		// if(Config.isSpecialProt) versionStr = "特殊端口\n" + versionStr;
		tv_settion_version.setText(versionStr);
	}

	/**
	 * 获取应用程序的版本号
	 * 
	 * @return 版本号
	 */
	private String getVersion() {
		try {
			PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
			return packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			return "1.0";
		}
	}

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

	@Override
	public void setListener() {
		lv_setting.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (presenter != null) {

					presenter.onItemClickListener(position);

				}
			}
		});
	}

	@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 || reqCode < 0)
			return;
		startActivityForResult(intent, reqCode);
	}

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

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

}
