//
//  TSGenmojiCollectionViewModel.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/17.
//


import ObjectMapper

class TSGenmojiColViewModel: TSBaseModel {
    
    lazy var generateSectionModel: TSGenmojiCoLSectionModel = {
        let generateSectionModel = TSGenmojiCoLSectionModel()
        generateSectionModel.name = ""
        generateSectionModel.items = [TSGenmojiCoLItemModel()]
        return generateSectionModel
    }()
    
}

//MARK: TSGenmojiCoLStyple
enum TSGenmojiCoLStyple : Int {
    case generate //创造 AI 图
    case history  //创造后的历史记录
    
    var sectionInset: UIEdgeInsets {
        switch self {
        case .generate:
            return UIEdgeInsets(top: 17, left: 17, bottom: 20, right: 15)
        case .history:
            return UIEdgeInsets(top: 0, left: 24, bottom: 10, right: 24)
        }
    }
        
    var lineSpacing: CGFloat {
        switch self {
        case .generate:
            return 0
        case .history:
            return 0
        }
    }
    
    var itemSpacing: CGFloat {
        switch self {
        case .generate:
            return 0
        case .history:
            return 16
        }
    }
    
    var cellSize:CGSize {
        switch self {
        case .generate:
            return CGSize(width: k_ScreenWidth-32, height: 303)
        case .history:
            return CGSize(width: 88, height: 88)
        }
    }
    
    var cellClass: UICollectionViewCell.Type {
        switch self {
        case .generate:
            return TSGenmojiGennerateCell.self
        case .history:
            return TSGenmojiItemCell.self
        }
    }
    
    var headerViewSize:CGSize {
        switch self {
        case .generate:
            return CGSize.zero
        case .history:
            return CGSizeMake(k_ScreenWidth, 60)
        }
    }
}


//MARK: TSGenmojiCoLSectionModel
class TSGenmojiCoLSectionModel: TSBaseModel {
    
    var style:TSGenmojiCoLStyple = .generate {
        didSet{
            for itemModel in items {
                itemModel.style = style
            }
        }
    }
    
    var name:String = "History"
    var items:[TSGenmojiCoLItemModel] = [TSGenmojiCoLItemModel]()
    override func mapping(map: ObjectMapper.Map) {
        style    <- map["style"]
        name    <- map["name"]
        items   <- map["items"]
    }

}


extension TSGenmojiCoLSectionModel : CollectionViewSectionComponent{
    var sectionInset: UIEdgeInsets {
        return style.sectionInset
    }
    
    var lineSpacing: CGFloat {
        return style.lineSpacing
    }
    
    var itemSpacing: CGFloat {
        return style.itemSpacing
    }
    var headerComponent: CollectionViewReuseViewComponent? {
        return TSGenmojiColComponentReuseViewModel(sectionModel: self)
    }
    
    var cells: [CollectionViewCellComponent] {
        return items
    }
}



//MARK: TSGenmojiCoLItemModel
class TSGenmojiCoLItemModel: TSBaseModel {
    var style:TSGenmojiCoLStyple = .generate
    
    var dataModel:TSGenmojiModel = TSGenmojiModel()
  
    override func mapping(map: ObjectMapper.Map) {
        dataModel    <- map["dataModel"]
        style    <- map["style"]
    }
}

extension TSGenmojiCoLItemModel: CollectionViewCellComponent{
    var cellClass: UICollectionViewCell.Type {
        style.cellClass
    }
    
    func cellSize(with attrubites: [String : Any]?) -> CGSize {
        style.cellSize
    }
}


//MARK: 分区头
class TSGenmojiColComponentReuseViewModel : TSBaseModel ,CollectionViewReuseViewComponent {
    var sectionModel:TSGenmojiCoLSectionModel

    init(sectionModel:TSGenmojiCoLSectionModel) {
        self.sectionModel = sectionModel
    }
    
    required convenience init?(json: [String : Any]) {
        fatalError("init(json:) has not been implemented")
    }
    
    required convenience init?(map: ObjectMapper.Map) {
        fatalError("init(map:) has not been implemented")
    }
    
    override func mapping(map: ObjectMapper.Map) {
        sectionModel    <- map["sectionModel"]
    }
    
    var viewClass: UICollectionReusableView.Type {
        return TSGenmojiColSectionView.self
    }
    
    var reuseIdentifier: String{
        return viewClass.description()
    }
    
    var viewSize: CGSize {
        return sectionModel.style.headerViewSize
    }
    
    var viewType: CollectionViewReuseViewType {
        return .header
    }
}
