package com.remax.visualnovel.widget.imagepicker.views.wrapper

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.PickerImageGridItemBinding
import com.remax.visualnovel.widget.imagepicker.bean.ImageItem
import com.remax.visualnovel.widget.imagepicker.bean.PickerItemDisableCode
import com.remax.visualnovel.widget.imagepicker.bean.selectconfig.BaseSelectConfig
import com.remax.visualnovel.widget.imagepicker.presenter.IPickerPresenter
import com.remax.visualnovel.widget.imagepicker.views.base.PickerItemView
import com.remax.visualnovel.widget.imagepicker.widget.ShowTypeImageView

/**
 * Created by HJW on 2022/10/21
 */
class ItemView constructor(context: Context?) : PickerItemView(context) {

    private var selectConfig: BaseSelectConfig? = null

    private var binding: PickerImageGridItemBinding? = null

    override fun getLayoutId(): Int {
        return R.layout.picker_image_grid_item
    }

    override fun initView(view: View?) {
        view?.run {
            binding = PickerImageGridItemBinding.bind(this.findViewById(R.id.group))
        }
    }

    override fun getCameraView(selectConfig: BaseSelectConfig?, presenter: IPickerPresenter?): View {
        return LayoutInflater.from(context).inflate(R.layout.picker_item_camera, null, false)
    }

    override fun getCheckBoxView(): View? {
        return binding?.mCheckBoxPanel
    }

    override fun initItem(imageItem: ImageItem?, presenter: IPickerPresenter?, selectConfig: BaseSelectConfig?) {
        this.selectConfig = selectConfig
        presenter?.displayImage(binding?.mImageView, imageItem, binding?.mImageView?.width ?: 0, true)
    }

    override fun disableItem(imageItem: ImageItem?, disableCode: Int) {
        //默认开启校验是否超过最大数,当超过最大选择数量时，
        if (disableCode == PickerItemDisableCode.DISABLE_OVER_MAX_COUNT) {
            return
        }
        binding?.mCheckBox?.isVisible = false
        binding?.vMasker?.isVisible = true
    }

    @SuppressLint("SetTextI18n")
    override fun enableItem(imageItem: ImageItem?, isChecked: Boolean, indexOfSelectedList: Int, isMax: Boolean) {
        binding?.run {
            /*imageItem?.let {
                if (it.isVideo) {
                    PickerImageGridItemBinding.mVideoLayout.isVisible = true
                    PickerImageGridItemBinding.mVideoTime.text = imageItem.getDurationFormat()
                    PickerImageGridItemBinding.mImageView.setType(ShowTypeImageView.TYPE_NONE)
                } else {
                    PickerImageGridItemBinding.mVideoLayout.isVisible = false
                    PickerImageGridItemBinding.mImageView.setTypeFromImage(imageItem)
                }


                PickerImageGridItemBinding.mCheckBox.isVisible = true
                PickerImageGridItemBinding.mCheckBoxPanel.isVisible = true

                val isVideoSinglePickAndAutoComplete = imageItem.isVideo && selectConfig!!.isVideoSinglePickAndAutoComplete
                if (isVideoSinglePickAndAutoComplete || selectConfig?.isSinglePickAutoComplete == true && (selectConfig?.maxCount ?: 0) <= 1) {
                    PickerImageGridItemBinding.mCheckBox.isVisible = false
                    PickerImageGridItemBinding.mCheckBoxPanel.isVisible = false
                }

                PickerImageGridItemBinding.mCheckBox.viewChecked(isChecked)
                PickerImageGridItemBinding.mCheckBox.text = if (isChecked) "${indexOfSelectedList + 1}" else null

                PickerImageGridItemBinding.singleCheckBox.isVisible = false



                if (isMax) {
                    PickerImageGridItemBinding.singleCheckBox.isVisible = indexOfSelectedList == 0
                    PickerImageGridItemBinding.vMasker.isVisible = !isChecked
                } else {
                    PickerImageGridItemBinding.vMasker.isVisible = false
                }
            }*/

        }

    }

}