package com.guadou.ninegrid.zipimg

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.ImageView
import com.guadou.lib_baselib.engine.extLoad
import com.guadou.ninegrid.R
import com.guadou.ninegrid.roundimg.NiceUtils
import java.util.*

/**
 * 组合的图片控件
 */
class ZipGridView : FrameLayout {

    private var mImageUrls: List<String?>? = null
    private val mImageViews: MutableList<ImageView> = ArrayList()

    constructor(context: Context?) : super(context!!)

    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context!!, attrs, defStyleAttr)

    /**
     * 设置图片集合
     */
    fun setImages(imgUrls: List<String?>?) {
        mImageUrls = imgUrls
        removeAllViews()

        imgUrls?.let {

            //只有一张图片
            if (mImageUrls!!.size == 1) {
                val imageView = getImageView(0)
                val layoutParams = LayoutParams(-1, -1)
                layoutParams.gravity = Gravity.CENTER
                imageView.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView, layoutParams)

                imageView.extLoad(it[0], R.drawable.news_feed_picture_placeholder)

            } else if (mImageUrls!!.size == 2) {
                //第一张和第二张，左右等分
                val imageView1 = getImageView(0)
                val layoutParams1 = LayoutParams(NiceUtils.dp2px(context, 49f), -1)
                layoutParams1.gravity = Gravity.LEFT
                imageView1.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView1, layoutParams1)
                val imageView2 = getImageView(1)
                val layoutParams2 = LayoutParams(NiceUtils.dp2px(context, 49f), -1)
                layoutParams2.gravity = Gravity.RIGHT
                imageView2.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView2, layoutParams2)

                //布局完成再设置加载
                imageView1.extLoad(it[0], R.drawable.news_feed_picture_placeholder)
                imageView2.extLoad(it[1], R.drawable.news_feed_picture_placeholder)

            } else if (mImageUrls!!.size == 3) {
                //第一张和第二三张，左右等分，第二和第三上下等分
                val imageView1 = getImageView(0)
                val layoutParams1 = LayoutParams(NiceUtils.dp2px(context, 49f), -1)
                layoutParams1.gravity = Gravity.LEFT
                imageView1.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView1, layoutParams1)
                val imageView2 = getImageView(1)
                val layoutParams2 =
                    LayoutParams(NiceUtils.dp2px(context, 49f), NiceUtils.dp2px(context, 49f))
                layoutParams2.gravity = Gravity.RIGHT or Gravity.TOP
                imageView2.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView2, layoutParams2)
                val imageView3 = getImageView(2)
                val layoutParams3 =
                    LayoutParams(NiceUtils.dp2px(context, 49f), NiceUtils.dp2px(context, 49f))
                layoutParams3.gravity = Gravity.RIGHT or Gravity.BOTTOM
                imageView3.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView3, layoutParams3)

                //布局完成再设置加载
                imageView1.extLoad(it[0], R.drawable.news_feed_picture_placeholder)
                imageView2.extLoad(it[1], R.drawable.news_feed_picture_placeholder)
                imageView3.extLoad(it[2], R.drawable.news_feed_picture_placeholder)

            } else if (mImageUrls!!.size > 3) {
                val imageView0 = getImageView(0)
                val layoutParams0 =
                    LayoutParams(NiceUtils.dp2px(context, 49f), NiceUtils.dp2px(context, 49f))
                layoutParams0.gravity = Gravity.LEFT or Gravity.TOP
                imageView0.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView0, layoutParams0)
                val imageView1 = getImageView(1)
                val layoutParams1 =
                    LayoutParams(NiceUtils.dp2px(context, 49f), NiceUtils.dp2px(context, 49f))
                layoutParams1.gravity = Gravity.LEFT or Gravity.BOTTOM
                imageView1.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView1, layoutParams1)
                val imageView2 = getImageView(2)
                val layoutParams2 =
                    LayoutParams(NiceUtils.dp2px(context, 49f), NiceUtils.dp2px(context, 49f))
                layoutParams2.gravity = Gravity.RIGHT or Gravity.TOP
                imageView2.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView2, layoutParams2)
                val imageView3 = getImageView(3)
                val layoutParams3 =
                    LayoutParams(NiceUtils.dp2px(context, 49f), NiceUtils.dp2px(context, 49f))
                layoutParams3.gravity = Gravity.RIGHT or Gravity.BOTTOM
                imageView3.scaleType = ImageView.ScaleType.CENTER_CROP
                addView(imageView3, layoutParams3)

                //布局完成再设置加载
                imageView0.extLoad(it[0], R.drawable.news_feed_picture_placeholder)
                imageView1.extLoad(it[1], R.drawable.news_feed_picture_placeholder)
                imageView2.extLoad(it[2], R.drawable.news_feed_picture_placeholder)
                imageView3.extLoad(it[3], R.drawable.news_feed_picture_placeholder)
            }

        }

    }

    /**
     * 获取图片，内存缓存避免重复创建ImageView
     */
    private fun getImageView(position: Int): ImageView {
        val imageView: ImageView
        if (position < mImageViews.size) {
            imageView = mImageViews[position]
        } else {
            imageView = ImageView(context)
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            mImageViews.add(imageView)
        }
        return imageView
    }
}