//
//  TSCollectionViewVM.swift
//  AIRingtone
//
//  Created by 100Years on 2025/2/28.
//

import ObjectMapper


//MARK: TSColVVMStyple
enum TSColVVMStyple : Int {
    case themeGuide     //主题引导
    case themeContent   //主题内容
    
    case posterHistory   //生成海报的历史记录
    case photoHistory    //生成头像的历史记录
    
    case ringCategories    //铃声分类
    case ringList       //铃声列表
    
    var config:TSColVVMSizeConfig {
        switch self {
        case .themeGuide:
            return themeGuideConfig
        case .themeContent:
            return themeContentConfig
        case .posterHistory:
            return posterHistoryConfig
        case .photoHistory:
            return photoHistoryConfig
        case .ringCategories:
            return ringCategoriesConfig
        case .ringList:
            return ringListConfig
        }
    }
    
    var sectionInset: UIEdgeInsets {
        config.sectionInset
    }
        
    var lineSpacing: CGFloat {
        config.lineSpacing
    }
    
    var itemSpacing: CGFloat {
        config.itemSpacing
    }

    
    var cellSize:CGSize {
        config.cellSize
    }
    
    var cellClass: UICollectionViewCell.Type {
        config.cellClass
    }
    
    var headerViewSize:CGSize {
        config.headerViewSize
    }
    
    var headerView:UICollectionReusableView.Type {
        config.headerView
    }
}


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

}


extension TSColVVMSectionModel : TSCollectionViewSectionComponent{
    var sectionInset: UIEdgeInsets {
        return style.sectionInset
    }
    
    var lineSpacing: CGFloat {
        return style.lineSpacing
    }
    
    var itemSpacing: CGFloat {
        return style.itemSpacing
    }
    var headerComponent: TSCollectionViewReuseViewComponent? {
        return TSColVVMComponentReuseViewModel(sectionModel: self)
    }
    
    var cells: [TSCollectionViewCellComponent] {
        return items
    }
}



//MARK: TSColVVMItemModel
class TSColVVMItemModel: TSBaseModel {
    var style:TSColVVMStyple = .themeContent
    var dataModel:TSBaseModel = TSBaseModel()

    override func mapping(map: ObjectMapper.Map) {
        dataModel           <- map["dataModel"]
        style               <- map["style"]
    }
}

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


//MARK: 分区头
class TSColVVMComponentReuseViewModel : TSBaseModel ,TSCollectionViewReuseViewComponent {
    var sectionModel:TSColVVMSectionModel
    
    var style:TSColVVMStyple = .themeContent
    var dataModel:TSBaseModel = TSBaseModel()
    
    init(sectionModel:TSColVVMSectionModel) {
        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 style.headerView
    }
    
    var reuseIdentifier: String{
        return viewClass.description()
    }
    
    var viewSize: CGSize {
        return sectionModel.style.headerViewSize
    }
    
    var viewType: TSCollectionViewReuseViewType {
        return .header
    }
}
