package com.muhron.playground.activity

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView

import com.bumptech.glide.Glide

import java.util.ArrayList
import java.util.Arrays

class ViewPagerExampleActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val viewPager = ViewPager(this)
        viewPager.adapter = SamplePagerAdapter(this, Arrays.asList(
                "http://placehold.jp/aaaaaa/ffffff/192x192.png",
                "http://placehold.jp/aa0000/ffffff/192x192.png",
                "http://placehold.jp/0000aa/ffffff/192x192.png",
                "http://placehold.jp/00aa00/ffffff/192x192.png"))
        setContentView(viewPager)
    }
}

internal class SamplePagerAdapter(private val context: Context, private val urls: List<String>) : PagerAdapter() {

    override fun instantiateItem(container: ViewGroup, position: Int): Any {

        val url = urls[position]
        val imageView = ImageView(context)
        Glide.with(this.context).load(url).into(imageView)
        container.addView(imageView)
        return imageView
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun getCount(): Int {
        return urls.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object` as ImageView
    }
}