package com.mjsheng.myappstore.dialog;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.image.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/29.
 */

public class AppImageDialog extends ShowDiaLog {


    private Context mContext;

    private ViewPager vpGuide;
    private ArrayList<View> mImageViewList;
    private List<String> vpList = null;
    private int position;


    public AppImageDialog(Context context, List<String> vpList, int position) {
        super(context, R.style.password_dialog_style);

        this.mContext = context;
        this.vpList = vpList;
        this.position = position;
        init();
    }

    private void init() {

        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_appstore_image, null);

        setContentView(view);
        vpGuide = (ViewPager) view.findViewById(R.id.vp_appstore_dialog_start);
        mImageViewList = new ArrayList<View>();
        // 初始化引导页的3个页面
        for (int i = 0; i < vpList.size(); i++) {
            View vpView = LayoutInflater.from(mContext).inflate(
                    R.layout.dialog_appstore_image_item, null);
            // 设置引导页背景
            ImageView iv = (ImageView) vpView.findViewById(R.id.iv_view_pager_img);
            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
            ImageLoader.getDefault().loadUrl(mContext, vpList.get(i),  iv);
            mImageViewList.add(vpView);
        }

        vpGuide.setAdapter(new AppImageDialog.GuideAdapter());
        vpGuide.setCurrentItem(position);
    }

    /**
     * ViewPager数据适配器
     */
    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return vpList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mImageViewList.get(position));
            return mImageViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }


}
