package com.yidianling.course.courseNew;

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

import com.ydl.ydl_image.module.GlideApp;
import com.yidianling.common.tools.RxImageTool;
import com.yidianling.course.R;
import com.yidianling.ydlcommon.utils.ScreenUtil;
import com.yidianling.ydlcommon.utils.UMEventUtils;
import com.yidianling.ydlcommon.utils.actionutil.ActionCountUtils;
import com.yidianling.ydlcommon.utils.actionutil.BIConstants;

import java.util.List;

/**
 * Created by softrice on 2015/11/13.
 */
public class CourseHeadPagerAdapter extends PagerAdapter {

    /**
     * 装ImageView数组
     */
    private List<BannerBean> banners;
    private ImageView[] mImageViews;
    private Context mContext;
    private int dp194, screenWidth;

    public CourseHeadPagerAdapter(List<BannerBean> banners, Context context) {
        this.banners = banners;
        mContext = context;
        dp194 = RxImageTool.dip2px(194f);
        screenWidth = ScreenUtil.screenWidth;
        initDatas();
    }

    public void setData(List<BannerBean> banners) {
        this.banners = banners;
        initDatas();
    }

    @Override
    public int getCount() {
        if (banners == null || banners.size() <= 0) {
            return 1;
        } else {
            return banners.size();
        }
    }

    private void initDatas() {
        if (banners == null || banners.size() <= 0) {
            mImageViews = new ImageView[1];
            mImageViews[0] = new ImageView(mContext);
            mImageViews[0].setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp194));
            mImageViews[0].setScaleType(ImageView.ScaleType.CENTER_CROP);
            GlideApp.with(mContext)
                    .load(R.drawable.default_banner)
                    .override(screenWidth, dp194)
                    .into(mImageViews[0]);
        } else {
            mImageViews = new ImageView[banners.size()];
            for (int i = 0; i < mImageViews.length; i++) {
                mImageViews[i] = new ImageView(mContext);
                mImageViews[i].setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp194));
                mImageViews[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
                GlideApp.with(mContext)
                        .load(banners.get(i).image)
                        .override(screenWidth, dp194)
                        .into(mImageViews[i]);
            }

        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView(mImageViews[position
                % mImageViews.length]);
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        try {
            ((ViewPager) container).addView(mImageViews[position
                    % mImageViews.length], 0);
        } catch (Exception e) {
            // handler something
        }
        mImageViews[position % mImageViews.length].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_BANNER_CLICK, position + "");

                LinkUrlRouterProtocol.jump(container.getContext(), banners.get(position).linkUrl);
            }
        });
        return mImageViews[position % mImageViews.length];
    }
}
