/*
 * Copyright 2014 trinea.cn All right reserved. This software is the confidential and proprietary information of
 * trinea.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with trinea.cn.
 */
package com.gh.gamecenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.gh.common.util.DataUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.NewsUtils;
import com.gh.gamecenter.GameDetailActivity;
import com.gh.gamecenter.NewsDetailActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.SubjectActivity;
import com.gh.gamecenter.entity.SlideEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ImagePagerAdapter
 *
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
 */
public class ImagePagerAdapter extends RecyclingPagerAdapter {

    private Context context;
    // private List<Integer> imageIdList;
    private List<SlideEntity> slideList = null;

    private int size;
    private boolean isInfiniteLoop;

    // private ImageIndicator indicator;

    public ImagePagerAdapter(Context context, List<SlideEntity> slideList,
                             boolean isInfiniteLoop) {
        this.context = context;
        this.slideList = slideList;
        this.size = getSize(slideList);
        this.isInfiniteLoop = isInfiniteLoop;
    }

    @Override
    public int getCount() {
        // Infinite loop
        return isInfiniteLoop ? Integer.MAX_VALUE : getSize(slideList);
    }

    public int getSize(List<SlideEntity> sourceList) {
        return sourceList == null ? 0 : sourceList.size();
    }

    /**
     * get really position
     *
     * @param position
     * @return
     */
    private int getPosition(int position) {
        return isInfiniteLoop ? position % size : position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = holder.imageView = new ImageView(context);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        final SlideEntity slideEntity = slideList.get(getPosition(position));

        ImageUtils.getInstance(context).display(slideEntity.getImage(),
                holder.imageView, R.drawable.preload);
        // indicator.setPosition(slideList.size(), getPosition(position));

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Map<String, Object> kv = new HashMap<String, Object>();
                kv.put("名字", slideEntity.getName());
                kv.put("位置", getPosition(position) + 1);
                DataUtils.onEvent(context, "点击", "游戏-插件-滚动图", kv);

                if ("game".equals(slideEntity.getType())) {
                    Intent intent = new Intent(context, GameDetailActivity.class);
                    intent.putExtra("gameId", slideEntity.getLink());
                    intent.putExtra("entrance", "游戏-插件-滚动图");
                    context.startActivity(intent);
                } else if ("news".equals(slideEntity.getType())) {
                    // 统计阅读量
                    NewsUtils.statNewsViews(slideEntity.getLink());

                    Intent intent = new Intent(context, NewsDetailActivity.class);
                    intent.putExtra("newsId", slideEntity.getLink());
                    intent.putExtra("entrance", "游戏-插件-滚动图");
                    context.startActivity(intent);
                } else if ("column".equals(slideEntity.getType())) {
                    Intent intent = new Intent(context, SubjectActivity.class);
                    intent.putExtra("id", slideEntity.getLink());
                    intent.putExtra("name", slideEntity.getName());
                    context.startActivity(intent);
                }

            }
        });

        return view;
    }

    private static class ViewHolder {
        ImageView imageView;
    }

    /**
     * @return the isInfiniteLoop
     */
    public boolean isInfiniteLoop() {
        return isInfiniteLoop;
    }

    /**
     * @param isInfiniteLoop
     *            the isInfiniteLoop to set
     */
    public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }
}
