package com.kfzs.duanduan.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.kfzs.duanduan.ActPicturesEnlarge;
import com.sheep.jiuyan.samllsheep.R;
import com.kfzs.duanduan.bean.KFIntentKeys;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by HooRang on 2017/2/16.
 */
public class PictureViewPagerAdapter extends PagerAdapter {

    public enum ViewPagerOpenModel {
        GAME_DETAILS,
        PICTURE_ENLARGE
    }

    private int mIntFirstShowPos = 0;
    private Activity that;
    public ViewPagerOpenModel openModel;
    private Map<Integer, String> mUrlsCollection;

    public int getmIntFirstShowPos() {
        return mIntFirstShowPos;
    }

    public void setmIntFirstShowPos(int mIntFirstShowPos) {
        this.mIntFirstShowPos = mIntFirstShowPos;
    }

    public PictureViewPagerAdapter(Activity ctx, Map<Integer, String> gameUrls, ViewPagerOpenModel model) {
        this.that = ctx;
        this.mUrlsCollection = gameUrls;
        this.openModel = model;
    }

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

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


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


    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        ImageView ivChildItem = new ImageView(that);
        View child = null;
        if (openModel == ViewPagerOpenModel.GAME_DETAILS) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            lp.gravity = Gravity.CENTER;
            ivChildItem.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            ivChildItem.setLayoutParams(lp);

            Glide.with(that)
                    .load(mUrlsCollection.get(position))
                    .apply(new RequestOptions().placeholder(R.drawable.loading)
                    .diskCacheStrategy(DiskCacheStrategy.ALL))
                    .into(ivChildItem);

            ivChildItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent picEnlargeIntent = new Intent(that, ActPicturesEnlarge.class);
                    picEnlargeIntent.putExtra(KFIntentKeys.EXTRA_CURRENT_PICTURE_INDEX, (position));
                    picEnlargeIntent.putExtra(KFIntentKeys.EXTRA_CURRENT_PICTURE_COLLECTIONS, (Serializable) mUrlsCollection);
                    that.startActivity(picEnlargeIntent);
                }
            });
            child = ivChildItem;
            container.addView(ivChildItem, position);

        } else if (openModel == ViewPagerOpenModel.PICTURE_ENLARGE) {
            RelativeLayout parent = new RelativeLayout(that);
            LinearLayout.LayoutParams lpParent = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            parent.setLayoutParams(lpParent);

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            lp.setMargins(20, 20, 20, 20);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            ivChildItem.setLayoutParams(lp);

            Glide.with(that).load(mUrlsCollection.get(position))
                    .apply(new RequestOptions().placeholder(R.drawable.loading)).into(ivChildItem);
            ivChildItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    that.finish();
                }
            });

            parent.addView(ivChildItem);
            child = parent;
            if (mIntFirstShowPos > 0 && container.getChildCount() == mIntFirstShowPos) {
                ((ViewPager) container).setCurrentItem(mIntFirstShowPos);
                mIntFirstShowPos = 0;//仅第一次设置生效
            }
            container.addView(parent, position);
        }


        return child;

    }


    @Override
    public float getPageWidth(int position) {
        return openModel == ViewPagerOpenModel.GAME_DETAILS ? (float) 0.4 : 1;
    }
}
