package com.adealink.weparty.profile.edit.comp

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.aab.util.getCompatString
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.diffutil.BaseListDiffUtil
import com.adealink.weparty.commonui.recycleview.itemdecoration.HorizontalItemDecoration
import com.adealink.weparty.module.image.data.PhotoData
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.LayoutEditProfilePhotoWallBinding
import com.adealink.weparty.profile.edit.adapter.AddPhotoItemViewBinder
import com.adealink.weparty.profile.edit.adapter.PhotoItemViewBinder
import com.adealink.weparty.profile.edit.data.BaseMediaItemData
import com.adealink.weparty.profile.edit.data.PhotoItemData
import com.adealink.weparty.profile.edit.viewmodel.EditProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
import com.adealink.weparty.util.goImagePreviewActivity

class EditPhotoWallComp(
    lifecycleOwner: LifecycleOwner,
    private val binding: LayoutEditProfilePhotoWallBinding
) : ViewComponent(lifecycleOwner) {

    companion object {
        private const val TAG = "EditPhotoWallComp"
        private const val PHOTO_SPAN = 3

    }

    private val editViewModel by activityViewModels<EditProfileViewModel> { ProfileViewModelFactory() }

    private val photoAdapter by fastLazy { MultiTypeListAdapter<BaseMediaItemData>(BaseListDiffUtil()) }

    private lateinit var photoTouchHelper: ItemTouchHelper
    private lateinit var photoTouchCallback: PhotoWallItemTouchHelperCallback
    private lateinit var takeFromAlbumComp: TakeFromAlbumComp
    override fun onCreate() {
        super.onCreate()
        initView()
        initViewComponent()
        observeViewModel()
    }

    private fun initView() {
        binding.tvTitle.text = getCompatString(
            R.string.profile_edit_photo_wall_title,
            "0",
            EditProfileViewModel.MAX_PHOTO_COUNT.toString()
        )

        photoAdapter.register(
            AddPhotoItemViewBinder {
                addPicture()
            }
        )
        photoAdapter.register(
            PhotoItemViewBinder(
                viewLifecycleOwner,
                onItemClick = {
                    previewPicture(it.data)
                },
                onDeleteClick = {
                    deletePicture(it.data)
                }
            ))
        binding.rvPhoto.adapter = photoAdapter
        binding.rvPhoto.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        binding.rvPhoto.addItemDecoration(
            HorizontalItemDecoration(10.dp(), 0, 0)
        )

        photoTouchCallback =
            PhotoWallItemTouchHelperCallback(
                {
                    editViewModel.photoList.isNotEmpty()
                },
                object : PhotoWallItemTouchHelperCallback.OnItemDragCallback {

                    override fun onMoved(fromPosition: Int, toPosition: Int) {
                        //调整位置
                        editViewModel.adjustPhotoPosition(fromPosition, toPosition)
                    }

                })
        photoTouchHelper = ItemTouchHelper(photoTouchCallback)
        photoTouchHelper.attachToRecyclerView(binding.rvPhoto)
    }

    private fun initViewComponent() {
        TakeFromAlbumComp(
            viewLifecycleOwner,
            clipParamData = null,
            onSelect = { path, uri ->
                editViewModel.addPhoto(path, uri)
            },
            source = "EditPhotoWallComp"
        ).also {
            takeFromAlbumComp = it
        }.attach()
    }

    private fun observeViewModel() {
        editViewModel.photoItemListLD.observe(viewLifecycleOwner) { itemList ->
            photoAdapter.submitList(itemList)
            binding.tvTitle.text = getCompatString(
                R.string.profile_edit_photo_wall_title,
                itemList.count { it is PhotoItemData }.toString(),
                EditProfileViewModel.MAX_PHOTO_COUNT.toString()
            )
        }
        editViewModel.adjustPhotoPositionLD.observe(viewLifecycleOwner) {
            photoAdapter.notifyItemMoved(it.first, it.second)
        }
    }

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

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

    private fun deletePicture(data: PhotoData) {
        editViewModel.removePhoto(data)
    }

}