package com.smart.gtstrvl;

import com.example.mountaintravel.adapter.MyPagerAdapter;
import com.example.mountaintravel.base.BaseActivity;
import com.example.mountaintravel.kind.MyCrashHandler;
import com.example.mountaintravel.util.UpdateManager;
import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class LeadActivity extends BaseActivity {

	private ViewPager vp;
	private MyPagerAdapter adapter;
	private Button btn_skip1;
	private Button btn_skip2;
	private SharedPreferences sp;
	private MyCrashHandler mCrashHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mCrashHandler = MyCrashHandler.getInstance();
		mCrashHandler.init(getApplicationContext());
		Thread.setDefaultUncaughtExceptionHandler(mCrashHandler);
		sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
		if (checkFirstRun()) {
			setContentView(R.layout.activity_lead);
			sp.edit().putBoolean("iferr11", false).commit();
			sp.edit().putBoolean("isFirstRun", false).commit();
			playLeadActivity();
			btn_skip1.setVisibility(View.VISIBLE);
			btn_skip1.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					startActivity(MainActivity.class, R.anim.left_in,
							R.anim.right_out);
					finish();
				}
			});
		} else {
			startActivity(new Intent(LeadActivity.this, StartActivity.class));
			finish();
		}
	}

	private void playLeadActivity() {
		// TODO Auto-generated method stub

		vp = (ViewPager) findViewById(R.id.vp);
		adapter = new MyPagerAdapter();
		vp.setAdapter(adapter);
		initViews();

		btn_skip1 = (Button) findViewById(R.id.btn_skip1);
		btn_skip2 = (Button) findViewById(R.id.btn_skip2);
		vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub

				/*
				 * Logo最后一页点击马上体验
				 */
				if (arg0 == adapter.getCount() - 1) {
					btn_skip2.setVisibility(View.VISIBLE);
					btn_skip2.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							startActivity(MainActivity.class, R.anim.left_in,
									R.anim.right_out);
							finish();
						}
					});
				} else {
					btn_skip2.setVisibility(View.GONE);
				}
				/*
				 * Logo第一页时点击一键跳过
				 */
				if (arg0 != 3) {
					btn_skip1.setVisibility(View.VISIBLE);

				} else {
					btn_skip1.setVisibility(View.GONE);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void initViews() {
		// TODO Auto-generated method stub
		ImageView i1 = new ImageView(this);
		// i1.setImageResource(R.drawable.app_start_1);
		i1.setImageDrawable(getResources().getDrawable(R.drawable.app_start_1));
		i1.setScaleType(ScaleType.FIT_XY);
		ImageView i2 = new ImageView(this);
		// i2.setImageResource(R.drawable.app_start_2);
		i2.setImageDrawable(getResources().getDrawable(R.drawable.app_start_2));
		i2.setScaleType(ScaleType.FIT_XY);
		ImageView i3 = new ImageView(this);
		// i3.setImageResource(R.drawable.app_start_3);
		i3.setImageDrawable(getResources().getDrawable(R.drawable.app_start_3));
		i3.setScaleType(ScaleType.FIT_XY);
		ImageView i4 = new ImageView(this);
		// i4.setImageResource(R.drawable.app_start_4);
		i4.setImageDrawable(getResources().getDrawable(R.drawable.app_start_4));
		i4.setScaleType(ScaleType.FIT_XY);
		adapter.addView(i1);
		adapter.addView(i2);
		adapter.addView(i3);
		adapter.addView(i4);
		adapter.notifyDataSetChanged();
	}

	private boolean checkFirstRun() {

		boolean isFirstRun = sp.getBoolean("isFirstRun", true);
		if (isFirstRun) {
			sp.edit().putBoolean("isFirstRun", false).commit();
			return true;
		} else {
			return false;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}