package com.adealink.weparty.order.refund.comp

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.mediapicker.imageselect.comp.TakeFromAlbumComp
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.commonui.text.setMaxLength
import com.adealink.weparty.constant.REFUND_REASON_MAX_LENGTH
import com.adealink.weparty.module.image.data.PhotoData
import com.adealink.weparty.order.databinding.LayoutOrderPlaymateRefundPlaymateBinding
import com.adealink.weparty.order.refund.PlaymateEvidenceActivity.Companion.PICTURE_SPAN
import com.adealink.weparty.order.refund.adapter.AddPhotoItemViewBinder
import com.adealink.weparty.order.refund.adapter.PhotoItemViewBinder
import com.adealink.weparty.order.refund.data.AddPhotoItemData
import com.adealink.weparty.order.refund.data.BasePhotoItemData
import com.adealink.weparty.order.viewmodel.OrderViewModelFactory
import com.adealink.weparty.order.viewmodel.PlaymateRefundViewModel
import com.adealink.weparty.util.QMUIKeyboardHelper
import com.adealink.weparty.util.goImagePreviewActivity

class PlaymateRefundPlaymateComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutOrderPlaymateRefundPlaymateBinding
) : ViewComponent(lifecycleOwner) {

    private val playmateRefundViewModel by activityViewModels<PlaymateRefundViewModel> { OrderViewModelFactory() }

    private val pictureAdapter by fastLazy { MultiTypeListAdapter<BasePhotoItemData>() }

    private lateinit var takeFromAlbumComp: TakeFromAlbumComp

    override fun onCreate() {
        super.onCreate()
        initView()
        initComponent()
        observeViewModel()
        loadData()
    }

    private fun initView() {
        binding.etInput.setMaxLength(REFUND_REASON_MAX_LENGTH)
        binding.etInput.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(
                s: CharSequence?,
                start: Int,
                count: Int,
                after: Int
            ) {
            }

            override fun onTextChanged(
                s: CharSequence?,
                start: Int,
                before: Int,
                count: Int
            ) {
            }

            @SuppressLint("SetTextI18n")
            override fun afterTextChanged(s: Editable?) {
                val input = s?.trim()?.toString()
                if (input.isNullOrEmpty()) {
                    binding.tvInputCount.text = "0/${REFUND_REASON_MAX_LENGTH}"
                } else {
                    binding.tvInputCount.text = "${input.length}/${REFUND_REASON_MAX_LENGTH}"
                }
            }
        })


        pictureAdapter.register(
            AddPhotoItemViewBinder {
                addPicture()
            }
        )
        pictureAdapter.register(
            PhotoItemViewBinder(
                onItemClick = {
                    previewPicture(it.data)
                },
                onDeleteClick = {
                    deletePicture(it.data)
                }
            ))
        binding.rvPicture.adapter = pictureAdapter
        binding.rvPicture.layoutManager = GridLayoutManager(context, PICTURE_SPAN)
        binding.rvPicture.addItemDecoration(
            GridSpacingItemDecoration(PICTURE_SPAN, 4.dp(), 4.dp(), false)
        )
    }

    private fun initComponent() {
        TakeFromAlbumComp(
            fragment ?: requireActivity(),
            clipParamData = null,
            onSelect = { path, uri ->
                playmateRefundViewModel.addPicture(path, uri)
            }
        ).also {
            takeFromAlbumComp = it
        }.attach()
    }

    private fun observeViewModel() {
        playmateRefundViewModel.pictureItemListLD.observe(this) {
            pictureAdapter.submitList(it)
        }
    }

    private fun loadData() {
        pictureAdapter.submitList(
            listOf(AddPhotoItemData)
        )
    }

    private fun addPicture() {
        takeFromAlbumComp.takeFromAlbum()
    }

    private fun previewPicture(data: PhotoData) {
        val uri = data.uri
        if (uri.isNullOrEmpty()) {
            return
        }
        val act = activity ?: return
        goImagePreviewActivity(
            act,
            arrayListOf(uri)
        )
    }

    private fun deletePicture(data: PhotoData) {
        playmateRefundViewModel.removePicture(data)
    }

    override fun onDestroy() {
        binding.etInput.clearFocus()
        QMUIKeyboardHelper.hideKeyboard(binding.etInput)
        super.onDestroy()
    }


    fun getPlaymateReason(): String? {
        return if (isValid) {
            binding.etInput.text?.trim()?.toString()
        } else {
            null
        }
    }
}