//
//  DiyPaperTemplate.swift
//  ClockWidget
//
//  Created by fff on 2024/6/28.
//

import ObjectMapper

// 模板
enum DiyPaperTemplate: String {
    case unknow
    case paleYellowFlowers
    case pinkAnimal

    
    var isLivePhoto: Bool {
        return false
    }
}

// 模板model
class DiyPaperTemplateModel: TSBaseModel {
    var template: DiyPaperTemplate = .unknow
    
    // 背景图片 | 背景色
    var background: String?
    
    // 字色
    var textColor: String?
    
    var isVip: Bool = false
    
    // 装饰元素
    var elements: [DiyPaperElement] = []
 
    override func mapping(map: Map) {
        template                <- map["template"]
        background              <- map["background"]
        textColor               <- map["textColor"]
        elements                <- map["elements"]
        isVip                   <- map["isVip"]
    }
}

// 模板元素动态model
class DiyPaperElement: TSBaseModel {
    enum `Type`: String {
        case text
        case sticker
        case photo
        
        // 固定装饰 - 不可编辑
        case decorate
        
        // 带底部容器的照片, 如拍立得卡片
        case photoContainer
        //固定位置的文字
        case fixedText
    }
    
    var type: `Type` = .text
    var element: TSBaseModel?
    
    override func mapping(map: Map) {
        type                <- map["type"]
        element             <- (map["element"], DiyElementTransformType(type: type))
    }
}

class DiyElementTransformType: TransformType {
    var type: DiyPaperElement.`Type`
    init(type: DiyPaperElement.`Type`) {
        self.type = type
    }
    
    typealias Object = TSBaseModel
    typealias JSON = [String: Any]
    
    func transformFromJSON(_ value: Any?) -> TSBaseModel? {
        guard let dict = value as? [String: Any] else {
            return nil
        }
        let className = type.elementClass
        return className.init(json: dict)
    }
    
    func transformToJSON(_ value: TSBaseModel?) -> [String : Any]? {
        return value?.toJSON() as? [String: Any]
    }
}

extension DiyPaperElement.`Type` {
    var elementClass: TSBaseModel.Type {
        switch self {
        case .text:
            return DiyTextElement.self
        case .sticker:
            return DiyStickerModel.self
        case .fixedText:
            return DiyFixedTextElement.self
//        case .photo:
//            return DiyPhotoElement.self
        case .decorate:
            return DiyStaticElement.self
//        case .photoContainer:
//            return DiyPhotoContainerElement.self
            
        default:
            return DiyStickerModel.self
        }
    }
}

//小图 239*515 设计稿375*812
let kDiyDesignScaleW = (239.0/375.0)// * kDesignScale
let kDiyDesignScaleH = kDiyDesignScaleW
