package com.cr.widget

import android.content.Context
import android.graphics.BitmapFactory
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.cr.cruav.R
import com.cr.data.CrUtil
import okhttp3.internal.notify
import java.io.FileInputStream
import java.io.FileNotFoundException

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/6/16 16:19
 * 描述：图片浏览器
 */
class CrImageBrowserWidget@JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet?=null,
    defStyleAttr: Int = 0
) :CrConstraintLayoutWidget(context,attrs,defStyleAttr){
    private var viewPager:ViewPager2?= null // define: 2023/6/16 页面滑动组件
    private var dataList:MutableList<String> = mutableListOf() // define: 2023/6/16 数据集合
    private var adapter:BaseAdapter?= null // define: 2023/6/16 适配器

    /**
     * 初始化
     */
    init {
        initView(R.layout.wg_image_browser)
        joinControls()
    }

    /**
     * 挂载控件
     */
    override fun joinControls() {
        super.joinControls()
        // todo: 2023/6/16 挂载视图控件
        viewPager = findViewById(R.id.wg_pager)
        // todo: 2023/6/16 设置适配器
        adapter = BaseAdapter(dataList)
        viewPager?.adapter = adapter
    }

    /**
     * 适配器
     * @property dataList List<String>?
     * @constructor
     */
    internal class BaseAdapter(dataList:List<String>):RecyclerView.Adapter<BaseAdapter.BaseViewHolder?>(){
        var dataList:List<String>? = null // define: 2023/6/16 数据集合

        // todo: 2023/6/16 创建视图
        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BaseAdapter.BaseViewHolder {
            var itemView:View = LayoutInflater.from(parent.context).inflate(R.layout.item_image,parent,false)
            return BaseViewHolder(itemView)
        }

        // todo: 2023/6/16 绑定视图
        override fun onBindViewHolder(holder: BaseAdapter.BaseViewHolder, position: Int) {
            var imagePath = this.dataList!![position]
            try {
                var fis = FileInputStream(imagePath)
                var bitmap = BitmapFactory.decodeStream(fis)
                holder.imageView?.setImageBitmap(bitmap)
            }catch (ex:FileNotFoundException){
                CrUtil.print("加载图片错误：" + ex.message)
            }
        }

        // todo: 2023/6/16 返回视图数量
        override fun getItemCount(): Int {
            return dataList!!.size
        }

        inner class BaseViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
            var imageView:ImageView?= null  // define: 2023/6/16 图片展示控件

            /**
             * 初始化
             */
            init {
                imageView = itemView.findViewById(R.id.item_img_image)
            }
        }

        /**
         * 初始化
         */
        init {
            this.dataList = dataList
        }

        /**
         * 更新数据
         * @param dataList List<String>
         */
        fun update(dataList: List<String>){
            this.dataList = dataList
            notifyDataSetChanged()
        }
    }

    /**
     * 添加图片
     * @param imagePath String 图片路径
     */
    fun crAppendImage(imagePath:String){
        this.dataList.add(imagePath)
        adapter?.update(this.dataList)
    }
}