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


import ObjectMapper

private let kTextPicHistoryScale = 165.0/293.0  //UI 设置尺寸
private let kTextPicHistoryW = k_ScreenWidth-32.0-12.0
private let kTextPicHistoryH = kTextPicHistoryW/kTextPicHistoryScale

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

//MARK: TSGenmojiCoLStyple
enum TSGenmojiCoLStyple : Int {
    case generate //创造Genmoji AI 图
    case history  //创造Genmoji 后的历史记录
    case textPicGenerate  //创造文生图后 AI 图
    case textPicHistory  //创造文生图后的历史记录
    var sectionInset: UIEdgeInsets {
        switch self {
        case .generate,.textPicGenerate:
            return UIEdgeInsets(top: 17, left: 17, bottom: 20, right: 15)
        case .history,.textPicHistory:
            return UIEdgeInsets(top: 0, left: 24, bottom: 10, right: 24)
        }
    }
        
    var lineSpacing: CGFloat {
        switch self {
        case .generate,.textPicGenerate:
            return 0
        case .history:
            return 16
        case .textPicHistory:
            return 13
        }
    }
    
    var itemSpacing: CGFloat {
        switch self {
        case .generate,.textPicGenerate:
            return 0
        case .history:
            return 16
        case .textPicHistory:
            return 13
        }
    }

    
    var cellSize:CGSize {
        switch self {
        case .generate,.textPicGenerate:
            return CGSize(width: k_ScreenWidth-32, height: 303)
        case .history:
            return CGSize(width: 88, height: 88)
        case .textPicHistory:
            return CGSize(width: kTextPicHistoryW, height: kTextPicHistoryH)
        }
    }
    
    var cellClass: UICollectionViewCell.Type {
        switch self {
        case .generate,.textPicGenerate:
            return TSGenmojiGennerateCell.self
        case .history,.textPicHistory:
            return TSGenmojiItemCell.self
        }
    }
    
    var headerViewSize:CGSize {
        switch self {
        case .generate,.textPicGenerate:
            return CGSize.zero
        case .history,.textPicHistory:
            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 : TSCollectionViewSectionComponent{
    var sectionInset: UIEdgeInsets {
        return style.sectionInset
    }
    
    var lineSpacing: CGFloat {
        return style.lineSpacing
    }
    
    var itemSpacing: CGFloat {
        return style.itemSpacing
    }
    var headerComponent: TSCollectionViewReuseViewComponent? {
        return TSGenmojiColComponentReuseViewModel(sectionModel: self)
    }
    
    var cells: [TSCollectionViewCellComponent] {
        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: TSCollectionViewCellComponent{
    var cellClass: UICollectionViewCell.Type {
        style.cellClass
    }
    
    func cellSize(with attrubites: [String : Any]?) -> CGSize {
        style.cellSize
    }
}


//MARK: 分区头
class TSGenmojiColComponentReuseViewModel : TSBaseModel ,TSCollectionViewReuseViewComponent {
    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: TSCollectionViewReuseViewType {
        return .header
    }
}
