//
//  TSImageDataTool.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/23.
//

import ObjectMapper


let columns = UIDevice.isPad ? 3.0 : 2.0
let itemMargin = columns*2//item的宽之间不能太精准,否则还是放不下,稍微去掉
private let liveBannerW = k_ScreenWidth
private let liveBannerH = 486.5

private let liveItemW = (k_ScreenWidth - 16 - 16 - 17)/columns - itemMargin
private let liveItemH =  kGetUIWdith(designSize: CGSize(width: 163.0, height: 352.0), currentW: liveItemW)

private let randomBannerW:CGFloat = k_ScreenWidth - 16.0
private let randowBannerH:CGFloat = kGetUIWdith(designSize:CGSize(width: 343, height: 117), currentW: randomBannerW)

private let randowItemW:CGFloat = (k_ScreenWidth - 13 - 16 - 16) / columns - itemMargin
private let randowItemH:CGFloat = kGetUIWdith(designSize:CGSize(width: 165, height: 278) , currentW: randowItemW)


enum ImageDataStyple : Int {
    case homeLiveBanner
    case homeLiveList
    case randomBanner
    case randomList
    
    
    var sectionInset: UIEdgeInsets {
        switch self {
        case .homeLiveBanner:
            return UIEdgeInsets(top: 24, left: 0, bottom: 17, right: 0)
        case .homeLiveList:
            return UIEdgeInsets(top: 0, left: 16, bottom: 10, right: 16)
        case .randomBanner:
            return UIEdgeInsets(top: 16, left: 16, bottom: 24, right: 16)
        case .randomList:
            return UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        }
    }
    
    var lineSpacing: CGFloat {
        switch self {
        case .homeLiveBanner:
            return 0
        case .homeLiveList:
            return 17
        case .randomBanner:
            return 0
        case .randomList:
            return 16
        }
    }
    
    var itemSpacing: CGFloat {
        switch self {
        case .homeLiveBanner:
            return 0
        case .homeLiveList:
            return 16
        case .randomBanner:
            return 0
        case .randomList:
            return 13
        }
    }
    
    var cellSize:CGSize {
        switch self {
        case .homeLiveBanner:
            return CGSize(width: liveBannerW, height: liveBannerH)
        case .homeLiveList:
            return CGSize(width: liveItemW, height: liveItemH)
        case .randomBanner:
            return CGSize(width: randomBannerW, height: randowBannerH)
        case .randomList:
            return CGSize(width: randowItemW, height: randowItemH)
        }
    }
    
    var cellClass: UICollectionViewCell.Type {
        switch self {
        case .homeLiveBanner:
            return TSHomeTopBannerCell.self
        case .homeLiveList:
            return TSHomeCollectionCell.self
        case .randomBanner:
            return TSRandomWallpaperBannerCell.self
        case .randomList:
            return TSRandomWallpaperCell.self
        }
    }
    
    var headerViewSize:CGSize {
        switch self {
        case .homeLiveBanner:
            return CGSize.zero
        case .homeLiveList:
            return CGSizeMake(k_ScreenWidth, 48)
        case .randomBanner:
            return CGSize.zero
        case .randomList:
            return CGSize.zero
        }
    }
}

let kImageDataCenterShared = TSImageDataCenter.shared
class TSImageDataCenter{

    static var shared = TSImageDataCenter()
    
    @UserDefault(key: "editLiveHistoryList", defaultValue: "")
    private var editLiveHistoryListString: String
    
    var editLiveHistoryListArray:[TSImageDataSectionModel]{
        get{
            if let totalArray = Mapper<TSImageDataSectionModel>().mapArray(JSONString: editLiveHistoryListString){
                return totalArray
            }else{
                let sectionModel = TSImageDataSectionModel()
                sectionModel.name = "History".localized
                return [sectionModel]
            }
        }
        
        set{
            if let jsonString = newValue.toJSONString() {
                editLiveHistoryListString = jsonString
            }
        }
    }
    
    
    

    //所有数据
    var totalArray:[TSImageDataSectionModel] = [TSImageDataSectionModel]()
    
    //banner 图片
    var liveBannerArray:[TSHomeBannerDataSectionModel] = [TSHomeBannerDataSectionModel]()
    //live 图片
    var liveListArray:[TSImageDataSectionModel] = [TSImageDataSectionModel]()
    
    
    //壁纸Banner
    var randomBannerArray:[TSRandomWallpaperDataSectionModel] = [TSRandomWallpaperDataSectionModel]()
    //壁纸组
    var randomListArray:[TSRandomWallpaperDataSectionModel] = [TSRandomWallpaperDataSectionModel]()
    
    init() {
    
        totalArray = [TSImageDataSectionModel]()
        
        liveListArray = [TSImageDataSectionModel]()
        randomListArray = [TSRandomWallpaperDataSectionModel]()
   
        if let totalArray = Mapper<TSImageDataSectionModel>().mapArray(JSONfile: "response.json"){
            
            self.totalArray = totalArray

            var randomItemListArray = [TSRandomWallpaperDataItemModel]()
            
            for sectionModel in totalArray {
                if sectionModel.type == "live" {
                    if let newModel = setSectionModelStype(sectionModel: sectionModel, style: .homeLiveList) {
                        liveListArray.append(newModel)
                    }
                }else{
                    if let newModel = getRandomWallpaperDataItemModel(sctionModel: sectionModel, style: .randomList) {
                        randomItemListArray.append(newModel)
                    }
                }
            }

            //首页顶部
            liveBannerArray = {
                let itemModel = TSHomeBannerDataItemModel()
                let randomInts = [16, 50, 27, 1, 57, 26, 31, 32, 15, 44]
                if liveBannerArray.count < 9 {
                    if let fistModel = liveListArray.first {
                        if let newModel = setSectionModelStype(sectionModel: fistModel, style: .homeLiveBanner) {
                            
                            var items = [TSImageDataItemModel]()
                            for randomInt in randomInts {
                                if let item = newModel.items.safeObj(At: randomInt) {
                                    items.append(item)
                                }
                            }
                            itemModel.items = items
                        }
                    }
                }

                let sectionModel = TSHomeBannerDataSectionModel()
                sectionModel.itemModels = [itemModel]
                sectionModel.style = .homeLiveBanner
                return [sectionModel]
            }()
            
            
            //随机壁纸 头
            randomBannerArray = {
                let sectionModel = TSRandomWallpaperDataSectionModel()
                sectionModel.itemModels = [TSRandomWallpaperDataItemModel()]
                sectionModel.style = .randomBanner
                return [sectionModel]
            }()
            
            //随机壁纸 list
            randomListArray = {
                let sectionModel = TSRandomWallpaperDataSectionModel()
                sectionModel.itemModels = randomItemListArray
                sectionModel.style = .randomList
                return  [sectionModel]
            }()
            
        }
    }
    
    func setSectionModelStype(sectionModel:TSImageDataSectionModel,style:ImageDataStyple) -> TSImageDataSectionModel?{
        if let newModel = sectionModel.copy() as? TSImageDataSectionModel{
            newModel.style = style
            return newModel
        }
        return nil
    }
    
    
    func getRandomWallpaperDataItemModel(sctionModel:TSImageDataSectionModel,style:ImageDataStyple) -> TSRandomWallpaperDataItemModel?{
        if let model = Mapper<TSRandomWallpaperDataItemModel>().map(JSON: sctionModel.toJSON()){
            model.style = style
            return model
        }
        return nil
    }
}

class TSImageDataSectionModel: TSBaseModel {
    var style:ImageDataStyple = .homeLiveList {
        didSet{
            for itemModel in items {
                itemModel.style = style
            }
        }
    }
    
    var type:String = ""
    var items:[TSImageDataItemModel] = [TSImageDataItemModel]()
    var name:String = "Recommend"
    var vip:Bool = false
    override func mapping(map: ObjectMapper.Map) {
        type    <- map["type"]
        items   <- map["items"]
        vip     <- map["vip"]
        style    <- map["style"]
    }
    
}

extension TSImageDataSectionModel : CollectionViewSectionComponent{
    
    var sectionInset: UIEdgeInsets {
        return style.sectionInset
    }
    
    var lineSpacing: CGFloat {
        return style.lineSpacing
    }
    
    var itemSpacing: CGFloat {
        return style.itemSpacing
    }
    var headerComponent: CollectionViewReuseViewComponent? {
        return TSComponentReuseViewModel(sectionModel: self)
    }
    
    var cells: [CollectionViewCellComponent] {
        
        if style == .homeLiveBanner {//用分区头来展示 banner
            return []
        }
        return items
    }
}
 


class TSImageDataItemModel: TSBaseModel {
    var style:ImageDataStyple = .homeLiveList
    
    var imageUrl:String = ""
    var videoUrl:String = ""
    var vip:Bool = false
    override func mapping(map: ObjectMapper.Map) {
        imageUrl    <- map["imageUrl"]
        videoUrl    <- map["videoUrl"]
        vip         <- map["vip"]
        
        style    <- map["style"]
    }
    
}

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


// 头部标题
class TSComponentReuseViewModel : TSBaseModel ,CollectionViewReuseViewComponent {
    var sectionModel:TSImageDataSectionModel

    init(sectionModel:TSImageDataSectionModel) {
        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 TSHomeCollectionReusableView.self
    }
    
    var reuseIdentifier: String{
        return viewClass.description()
    }
    
    var viewSize: CGSize {
        return sectionModel.style.headerViewSize
    }
    
    var viewType: CollectionViewReuseViewType {
        return .header
    }
}
