/*
 * 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.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.gamecenter.common.entity.ExposureEntity;
import com.gh.common.exposure.ExposureEvent;
import com.gh.common.exposure.ExposureSource;
import com.gh.common.exposure.ExposureType;
import com.gh.gamecenter.common.utils.DataLogUtils;
import com.gh.common.util.DirectUtils;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.gamecenter.core.utils.PageSwitchDataHelper;
import com.gh.gamecenter.core.utils.StringUtils;
import com.gh.gamecenter.MainActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.common.entity.LinkEntity;
import com.gh.gamecenter.entity.SubjectRecommendEntity;
import com.gh.gamecenter.game.data.GameItemData;

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

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

    private Context mContext;
    private List<LinkEntity> mSlideEntityList;

    private int mSize;
    private boolean mIsInfiniteLoop;
    private SubjectRecommendEntity mBlockData;

    private String mSource;

    private GameItemData mItemData;
    private ArrayList<ExposureSource> mExposureSourceList;

    public ImagePagerAdapter(Context context, GameItemData itemData, boolean isInfiniteLoop, List<ExposureSource> basicExposureSource, SubjectRecommendEntity blockData) {
        mContext = context;
        mSlideEntityList = itemData.getSlideList();
        mSize = getSize(itemData.getSlideList());
        mIsInfiniteLoop = isInfiniteLoop;
        mSource = blockData.getName();
        mBlockData = blockData;
        mItemData = itemData;
        mItemData.setExposureEventList(new ArrayList<>());
        mExposureSourceList = new ArrayList<>();
        mExposureSourceList.addAll(basicExposureSource);
        mExposureSourceList.add(new ExposureSource("轮播图", ""));
    }

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

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

    @Override
    public View getView(final int position, View view, ViewGroup container) {
        if (view == null) {
            view = new SimpleDraweeView(mContext);
        }

        int index = getPosition(position);
        if (index >= mSlideEntityList.size()) return view;

        final LinkEntity slideEntity = mSlideEntityList.get(index);

        ImageUtils.display(mContext.getResources(), (SimpleDraweeView) view, slideEntity.getImage(), R.drawable.preload);

        ExposureEvent exposureEvent = null;
        if ("game".equals(slideEntity.getType())) {
            GameEntity fakeGameEntity = new GameEntity();
            fakeGameEntity.setId(slideEntity.getLink());
            fakeGameEntity.setName(slideEntity.getText());
            fakeGameEntity.setSequence(index);
            fakeGameEntity.setContainerId(mBlockData.getLink());
            fakeGameEntity.setContainerType(ExposureEntity.BLOCK_ID);

            exposureEvent = ExposureEvent.createEvent(fakeGameEntity, mExposureSourceList, null, ExposureType.EXPOSURE);
        }

        ExposureEvent finalExposureEvent = exposureEvent;
        view.setOnClickListener(v -> {
            int size = mSlideEntityList.size();

            if (size == 0) return;

            // 轮播图数据统计
            if (mContext instanceof MainActivity) {
                DataLogUtils.uploadLunbotuLog(mContext, slideEntity.getType(), slideEntity.getText(), "", String
                        .valueOf(getPosition(position) % size + 1), "游戏库");
            } else {
                DataLogUtils.uploadLunbotuLog(mContext, slideEntity.getType(), slideEntity.getText(), "", String
                        .valueOf(getPosition(position) % size + 1), mSource);
            }

            String indexName = String.valueOf(getPosition(position) % size + 1);
            String entrance = StringUtils.buildString("(", mSource, "-轮播图[", slideEntity.getText(), "=", slideEntity
                    .getType(), "=", indexName, "])");

            MtaHelper.onEvent("轮播图", mSource, indexName);

            HashMap<String, String> map = new HashMap<>();
            map.put(PageSwitchDataHelper.PAGE_BUSINESS_TYPE, "版块轮播图");
            map.put(PageSwitchDataHelper.PAGE_BUSINESS_NAME, mBlockData.getName());
            map.put(PageSwitchDataHelper.PAGE_BUSINESS_ID, mBlockData.getLink());
            PageSwitchDataHelper.pushCurrentPageData(map);

            DirectUtils.INSTANCE.directToLinkPage(mContext, slideEntity, entrance, "轮播图[" + slideEntity
                    .getText() + "=" + slideEntity.getType() + "=" + indexName + "]", finalExposureEvent);
        });

        mItemData.getExposureEventList().add(finalExposureEvent);

        return view;
    }

    /**
     * get real position
     */
    private int getPosition(int position) {
        return mIsInfiniteLoop ? position % mSize : position;
    }

    public int getDataSize() {
        return mSlideEntityList == null ? 0 : mSlideEntityList.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

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

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

    public void checkResetData(GameItemData itemData) {
        if (mSlideEntityList != itemData.getSlideList()) {
            mSlideEntityList = itemData.getSlideList();
            mSize = getSize(itemData.getSlideList());
        }

        if (mItemData.getExposureEventList() != null) {
            mItemData.getExposureEventList().clear();
            mItemData = itemData;
            mItemData.setExposureEventList(new ArrayList<>());
        }

        notifyDataSetChanged();
    }
}
