package com.danding.yang800;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.danding.yang800.Adapter.MyPagerAdapter;
import com.danding.yang800.Util.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LeadActivity extends BaseActivity {
    @BindView(R.id.lead_viewpager)
    ViewPager lead_viewpager;
    @BindView(R.id.skip_Image)
    ImageView skip_Image;
    @BindView(R.id.start_Image)
    ImageView start_Image;
    private SharedPreferences sp;
    private MyPagerAdapter adapter;
    private ImageView[] dots;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lead);
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
//        if (checkFirstRun()) {
        setContentView(R.layout.activity_lead);
        ButterKnife.bind(this);
        sp.edit().putBoolean("isFirstRun", false).commit();
        playLeadActivity();
        skip_Image.setVisibility(View.VISIBLE);
        skip_Image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sp.getBoolean("login", false)) {
                    startActivity(MainActivity.class);
                    finish();
                } else {
                    startActivity(LoginActivity.class);
                    finish();
                }
            }
        });
//        } else {
//            startActivity(new Intent(LeadActivity.this, MainActivity.class));
//            finish();
//        }
    }

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

        adapter = new MyPagerAdapter();
        lead_viewpager.setAdapter(adapter);
        initViews();
        dots = new ImageView[adapter.getCount()];
        dots[0] = (ImageView) findViewById(R.id.dot0);
        dots[1] = (ImageView) findViewById(R.id.dot1);
        lead_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                for (int i = 0; i < adapter.getCount(); i++) {
                    dots[i].setImageResource(R.drawable.dot_gray);
                }
                dots[arg0].setImageResource(R.drawable.dot_red);
                /*
                 * Logo最后一页点击马上体验
				 */
                if (arg0 == adapter.getCount() - 1) {
                    start_Image.setVisibility(View.VISIBLE);
                    start_Image.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            if (sp.getBoolean("login", false)) {
                                startActivity(MainActivity.class);
                                finish();
                            } else {
                                startActivity(LoginActivity.class);
                                finish();
                            }

                        }
                    });
                } else {
                    start_Image.setVisibility(View.GONE);
                }
                /*
                 * Logo第一页时点击一键跳过
				 */
                if (arg0 != 1) {
                    skip_Image.setVisibility(View.VISIBLE);

                } else {
                    skip_Image.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(ImageView.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(ImageView.ScaleType.FIT_XY);
        adapter.addView(i1);
        adapter.addView(i2);
        adapter.notifyDataSetChanged();
    }

    private boolean checkFirstRun() {

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