package com.veinhorn.scrollgalleryview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

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

/**
 * Created by veinhorn on 6.8.15.
 */
public class ScrollGalleryView extends LinearLayout {
    private FragmentManager fragmentManager;
    private Context context;
    private WindowManager windowManager;
    private int thumbnailSize; // width and height in pixels

    // Views
    private LinearLayout thumbnailsContainer;
    private HorizontalScrollView horizontalScrollView;
    private ViewPager viewPager;
    ////////
    private PagerAdapter pagerAdapter;
    private List<Integer> images;


    public ScrollGalleryView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        images = new ArrayList<>();

        setOrientation(VERTICAL);
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();

        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.scroll_gallery_view, this, true);

        horizontalScrollView = (HorizontalScrollView)findViewById(R.id.thumbnails_scroll_view);

        thumbnailsContainer = (LinearLayout)findViewById(R.id.thumbnails_container);
        thumbnailsContainer.setPadding(display.getWidth() / 2, 0,
                display.getWidth() / 2, 0);
    }

    public ScrollGalleryView setFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        initializeViewPager();
        return this;
    }

    public ScrollGalleryView addImage(int image) {
        images.add(image);
        addThumbnail(image);
        pagerAdapter.notifyDataSetChanged();
        return this;
    }

    public ScrollGalleryView setThumbnailSize(int thumbnailSize) {
        this.thumbnailSize = thumbnailSize;
        return this;
    }

    private OnClickListener thumbnailOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            scroll(v);
            viewPager.setCurrentItem((int)v.getTag(), true);
        }
    };

    private ViewPager.SimpleOnPageChangeListener viewPagerChangeListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            scroll(thumbnailsContainer.getChildAt(position));
        }
    };

    private ScrollGalleryView addThumbnail(int image) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(thumbnailSize, thumbnailSize);
        lp.setMargins(10, 10, 10, 10);
        Bitmap thumbnail = createThumbnail(image);
        thumbnailsContainer.addView(createThumbnailView(lp, thumbnail));
        return this;
    }

    private ImageView createThumbnailView(LinearLayout.LayoutParams lp, Bitmap thumbnail) {
        ImageView thumbnailView = new ImageView(context);
        thumbnailView.setLayoutParams(lp);
        thumbnailView.setImageBitmap(thumbnail);
        thumbnailView.setTag(images.size() - 1);
        thumbnailView.setOnClickListener(thumbnailOnClickListener);
        return thumbnailView;
    }

    private Bitmap createThumbnail(int image) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), image);
        return ThumbnailUtils.extractThumbnail(bitmap, thumbnailSize, thumbnailSize);
    }

    private void initializeViewPager() {
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        pagerAdapter = new ScreenSlidePagerAdapter(fragmentManager, images);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(viewPagerChangeListener);
    }

    private void scroll(View thumbnail) {
        Display display = windowManager.getDefaultDisplay();
        int thumbnailCoords[] = new int [2];
        thumbnail.getLocationOnScreen(thumbnailCoords);

        int thumbnailCenterX = thumbnailCoords[0] + thumbnailSize / 2;
        int thumbnailDelta = display.getWidth() / 2 - thumbnailCenterX;

        horizontalScrollView.smoothScrollBy(-thumbnailDelta, 0);
    }
}

@@ -137,16 +137,47 @@
 actory;%0A
+import android.graphics.Point;%0A
 import a
@@ -205,16 +205,16 @@
 lUtils;%0A
-
 import a
@@ -851,35 +851,26 @@
 ate 
-WindowManager windowManager
+Point displayProps
 ;%0A  
@@ -1089,16 +1089,17 @@
 ///////%0A
+%0A
     priv
@@ -1379,147 +1379,33 @@
     
-windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);%0A        Display display = windowManager.getDefaul
+displayProps = ge
 tDisplay
 ();%0A
@@ -1400,21 +1400,24 @@
 tDisplay
+Size
 ();%0A
-%0A
         
@@ -1765,35 +1765,31 @@
 ding(display
-.getWidth()
+Props.x
  / 2, 0,%0A   
@@ -1804,35 +1804,31 @@
      display
-.getWidth()
+Props.x
  / 2, 0);%0A  
@@ -2541,16 +2541,17 @@
 em((int)
+ 
 v.getTag
@@ -2840,32 +2840,330 @@
       %7D%0A    %7D;%0A%0A
+    private Point getDisplaySize() %7B%0A        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);%0A        Display display = windowManager.getDefaultDisplay();%0A        Point point = new Point();%0A        display.getSize(point);%0A        return point;%0A    %7D%0A%0A
     private Scro
@@ -4451,69 +4451,8 @@
 ) %7B%0A
-        Display display = windowManager.getDefaultDisplay();%0A
     
@@ -4660,19 +4660,15 @@
 play
-.getWidth()
+Props.x
  / 2

improve perfomance #2