//
//  LNRoomGiftListView.swift
//  Gami
//
//  Created by OneeChan on 2026/3/23.
//

import Foundation
import UIKit
import SnapKit
import Combine


protocol LNRoomGiftListViewDelegate: AnyObject {
    func onRoomGiftListView(_ view: LNRoomGiftListView, didSelect index: Int)
}


class LNRoomGiftListView: UIView {
    private let collectionView: UICollectionView
    private var selectedIndex: Int?
    var selectedGift: LNGiftItemVO? {
        if let selectedIndex {
            giftList[selectedIndex]
        } else {
            nil
        }
    }
    
    private var roomSession: LNRoomSession?
    private var giftList: [LNGiftItemVO] = []
    
    weak var delegate: LNRoomGiftListViewDelegate?
    
    override init(frame: CGRect) {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 0
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ room: LNRoomSession?) {
        roomSession = room
        giftList = roomSession?.giftList ?? [] // 拷贝一份，避免房间数据更新后导致界面异常 
        selectedIndex = nil
        collectionView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNRoomGiftListView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        giftList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LNRoomGiftItemCell.className, for: indexPath) as! LNRoomGiftItemCell
        cell.update(giftList[indexPath.item])
        cell.isSelected = selectedIndex == indexPath.item
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if selectedIndex == indexPath.item { return }
        
        if let selectedIndex {
            collectionView.cellForItem(at: .init(row: selectedIndex, section: 0))?.isSelected = false
        }
        selectedIndex = indexPath.item
        collectionView.cellForItem(at: indexPath)?.isSelected = true
        (collectionView.cellForItem(at: indexPath) as? LNRoomGiftItemCell)?.showJumpAnimate()
        
        delegate?.onRoomGiftListView(self, didSelect: indexPath.item)
    }
    
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        .init(width: 90, height: 96)
    }
}

private extension LNRoomGiftListView {
    func setupViews() {
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.allowsMultipleSelection = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.contentInset = .init(top: 0, left: 7.5, bottom: 32, right: 7.5)
        collectionView.register(LNRoomGiftItemCell.self, forCellWithReuseIdentifier: LNRoomGiftItemCell.className)
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let bottomGradientView = UIView.gradientView([
            .init(hex: "#24213800"), .init(hex: "#242138")
        ], .verticalUTD)
        addSubview(bottomGradientView)
        bottomGradientView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(44)
        }
    }
}
