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

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
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.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.imageselect.comp.TakeFromAlbumComp
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.BasePhotoItemData
import com.adealink.weparty.profile.edit.data.PhotoData
import com.adealink.weparty.profile.edit.viewmodel.EditProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
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 profileViewModel by activityViewModels<ProfileViewModel> { ProfileViewModelFactory() }
    private val editViewModel by activityViewModels<EditProfileViewModel> { ProfileViewModelFactory() }

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

    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() {
        photoAdapter.register(
            AddPhotoItemViewBinder {
                addPicture()
            }
        )
        photoAdapter.register(
            PhotoItemViewBinder(
                onItemClick = {
                    previewPicture(it.data)
                },
                onDeleteClick = {
                    deletePicture(it.data)
                }
            ))
        binding.rvPhoto.adapter = photoAdapter
        binding.rvPhoto.layoutManager = GridLayoutManager(context, PHOTO_SPAN)
        binding.rvPhoto.addItemDecoration(
            GridSpacingItemDecoration(PHOTO_SPAN, 4.dp(), 4.dp(), false)
        )

        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, null) { path, uri ->
            editViewModel.addPhoto(path, uri)
        }.also {
            takeFromAlbumComp = it
        }.attach()
    }

    private fun observeViewModel() {
        editViewModel.photoItemListLD.observe(viewLifecycleOwner) {
            photoAdapter.submitList(it)
        }
        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.isRemoteImage()) {
            goImagePreviewActivity(
                act,
                arrayListOf(data.url ?: "")
            )
        } else {
            goImagePreviewActivity(
                act,
                arrayListOf(data.uri ?: "")
            )
        }
    }

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

}