package com.kfzs.duanduan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.kfzs.duanduan.bean.KFIntentKeys;
import com.kfzs.duanduan.fragment.PictureViewPagerAdapter;
import com.kfzs.duanduan.view.DepthPageTransformer;
import com.sheep.jiuyan.samllsheep.R;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by HooRang on 2017/3/22.
 */
public class ActPicturesEnlarge extends BaseCompatActivity {

    private Bundle extras;

    @BindView(R.id.picture_enlarge_viewpager)
    ViewPager vpPictureEnlarge;

    @BindView(R.id.picture_enlarge_label)
    TextView tvLabel;

    Map<Integer, String> mUrlCollections;
    int iCurrentItem = 1;

    PictureViewPagerAdapter picViewPagerAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_enlarge);
        ButterKnife.bind(this);

        extras = getIntent().getExtras();
        if(extras == null) {
            finish();
            return;
        }
        String imgs = null;
        if((imgs = extras.getString("imgs")) != null){
            String[] items = imgs.split(";");
            mUrlCollections = new HashMap<>();
            for (int i = 0; i < items.length; i++) {
                mUrlCollections.put(i,items[i]);
            }
        } else {
            mUrlCollections = (Map<Integer, String>) extras.getSerializable(KFIntentKeys.EXTRA_CURRENT_PICTURE_COLLECTIONS);
        }
        iCurrentItem = extras.getInt(KFIntentKeys.EXTRA_CURRENT_PICTURE_INDEX);

        initActivityUI();

    }


    private void initActivityUI() {
        tvLabel.setText(formatLabel(iCurrentItem + 1));

        picViewPagerAdapter = new PictureViewPagerAdapter(this, mUrlCollections, PictureViewPagerAdapter.ViewPagerOpenModel.PICTURE_ENLARGE);
        vpPictureEnlarge.setOffscreenPageLimit(mUrlCollections.size());
        vpPictureEnlarge.setPageMargin(15);
        vpPictureEnlarge.setAdapter(picViewPagerAdapter);
        picViewPagerAdapter.setmIntFirstShowPos(iCurrentItem);
        vpPictureEnlarge.setPageTransformer(true, new DepthPageTransformer());


        vpPictureEnlarge.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvLabel.setText(formatLabel(position + 1));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    private String formatLabel(int iCurrentIndex) {
        return iCurrentIndex + "/" + (mUrlCollections == null ? 0 : mUrlCollections.size());
    }

}
