//
//  TSViewController.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/13.
//

import UIKit
import SwipeCellKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, SwipeCollectionViewCellDelegate {
    
    // MARK: - UI Components
    private var collectionView: UICollectionView!
    
    // MARK: - Data
    private var data: [String] = [
        "Cell 1", "Cell 2", "Cell 3", "Cell 4", "Cell 5",
        "Cell 6", "Cell 7", "Cell 8", "Cell 9", "Cell 10"
    ]
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    // MARK: - Setup UI
    private func setupUI() {
        view.backgroundColor = .white
        title = "SwipeCellKit with UICollectionView"
        
        // 设置 UICollectionViewLayout
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: view.frame.width, height: 60) // 每个 Cell 的宽度为屏幕宽度
        layout.minimumLineSpacing = 10 // 行间距
        layout.scrollDirection = .vertical
        
        // 初始化 UICollectionView
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CustomSwipeCell.self, forCellWithReuseIdentifier: "CustomSwipeCell")
        
        // 添加 UICollectionView 到视图
        view.addSubview(collectionView)
        
        // 设置约束
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
    
    // MARK: - UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomSwipeCell", for: indexPath) as! CustomSwipeCell
        cell.textLabel.text = data[indexPath.row]
        cell.delegate = self // 设置 SwipeCollectionViewCellDelegate
        return cell
    }
    
    // MARK: - SwipeCollectionViewCellDelegate
    func collectionView(_ collectionView: UICollectionView, editActionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        guard orientation == .right else { return nil }
        
        // 删除操作
        let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
            print("Delete action for cell \(indexPath.row)")
            self.data.remove(at: indexPath.row) // 删除数据源
            self.collectionView.deleteItems(at: [indexPath]) // 删除 Cell
        }
        deleteAction.backgroundColor = .red
        deleteAction.image = UIImage(systemName: "trash")
        
        // 更多操作
        let moreAction = SwipeAction(style: .default, title: "More") { action, indexPath in
            print("More action for cell \(indexPath.row)")
        }
        moreAction.backgroundColor = .blue
        moreAction.image = UIImage(systemName: "ellipsis")
        
        return [deleteAction, moreAction]
    }
    
    func collectionView(_ collectionView: UICollectionView, editActionsOptionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
        var options = SwipeOptions()
        options.expansionStyle = .destructive(automaticallyDelete: false) // 完全滑动时是否自动触发操作
        options.transitionStyle = .border // 滑动动画样式
        return options
    }
}

// MARK: - CustomSwipeCell
class CustomSwipeCell: SwipeCollectionViewCell {
    
    // 文本标签
    let textLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        label.textColor = .black
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = .lightGray
        layer.cornerRadius = 8
        clipsToBounds = true
        // 添加文本标签
        contentView.addSubview(textLabel)
        
        // 设置约束
        NSLayoutConstraint.activate([
            textLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
            textLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor)
        ])
    }
}
