package com.adealink.weparty.setting.helpcenter

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.GridLayoutManager
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.naviBarHeight
import com.adealink.frame.util.onClick
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.imageselect.comp.TakeFromAlbumComp
import com.adealink.weparty.module.setting.Setting
import com.adealink.weparty.setting.databinding.ActivityHelpCenterBinding
import com.adealink.weparty.setting.helpcenter.adapter.AddPictureItemViewBinder
import com.adealink.weparty.setting.helpcenter.adapter.PictureItemViewBinder
import com.adealink.weparty.setting.helpcenter.data.AddPictureItemData
import com.adealink.weparty.setting.helpcenter.data.BasePictureItemData
import com.adealink.weparty.setting.helpcenter.data.PictureData
import com.adealink.weparty.setting.helpcenter.viewmodel.HelpCenterViewModel
import com.adealink.weparty.util.goImagePreviewActivity
import com.qmuiteam.qmui.widget.util.QMUIKeyboardHelper
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Setting.HelpCenter.PATH],
    desc = "帮助中心"
)
class HelpCenterActivity : BaseActivity() {

    companion object {
        private const val MAX_INPUT_COUNT = 800
        private const val PICTURE_SPAN = 3
    }

    private val binding by viewBinding(ActivityHelpCenterBinding::inflate)

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

    private val viewModel by viewModels<HelpCenterViewModel>()

    private lateinit var takeFromAlbumComp: TakeFromAlbumComp

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        val statusBarHeight = statusBarHeight()
        val naviBarHeight = naviBarHeight()
        binding.topBar.setPadding(0, statusBarHeight, 0, 0)
        binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height = statusBarHeight + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt()
        }
        binding.btnSubmit.updateLayoutParams<ConstraintLayout.LayoutParams> {
            bottomMargin = naviBarHeight + 24.dp()
        }
        binding.clContent.setPadding(
            0, 0, 0,
            getCompatDimension(APP_R.dimen.common_button_height).toInt() + naviBarHeight + 48.dp()
        )

        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.btnSubmit.isEnabled = false
                    binding.tvInputCount.text = "0/${MAX_INPUT_COUNT}"
                } else {
                    binding.btnSubmit.isEnabled = true
                    binding.tvInputCount.text = "${input.length}/${MAX_INPUT_COUNT}"
                }
            }
        })


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

        binding.btnSubmit.onClick {
            submit()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.pictureItemListLD.observe(this) {
            pictureAdapter.submitList(it)
        }
    }

    override fun loadData() {
        super.loadData()
        pictureAdapter.submitList(
            listOf(AddPictureItemData)
        )
    }

    override fun initComponents() {
        super.initComponents()
        TakeFromAlbumComp(this, null) { path, uri ->
            viewModel.addPicture(path, uri)
        }.also {
            takeFromAlbumComp = it
        }.attach()
    }

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

    private fun previewPicture(data: PictureData) {
        if (data.uri.isNullOrEmpty()) {
            return
        }
        goImagePreviewActivity(
            this,
            arrayListOf(data.uri)
        )
    }

    private fun deletePicture(data: PictureData) {
        viewModel.removePicture(data)
    }

    private fun submit() {
        viewModel.submit()
    }

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

}