//
//  KMHeaderFooterAdjectiveModel.swift
//  PDF Reader Pro
//
//  Created by tangchao on 2022/12/27.
//

import Cocoa

enum KMHeaderFooterObjectType: Int {
    case headerFooter = 0
    case bates
}

@objcMembers class KMHeaderFooterAdjectiveModel: KMWatermarkAdjectiveBaseModel , NSCoding {
    var type: KMHeaderFooterObjectType = .headerFooter
    
    // 是否数据迁移(原App数据迁移)
    var isMigrate = false
    // 是否已被更新
    var isUpdated = false
    
    var leftMargin: Int = 30
    var rightMargin: Int = 30
    var bottomMargin: Int = 30
    var topMargin: Int = 30
    var topLeftString: String = ""
    var topCenterString: String = ""
    var topRightString: String = ""
    var bottomLeftString: String = ""
    var bottomCenterString: String = ""
    var bottomRightString: String = ""
    
    var startString: String = "1"
    
    var name: String = ""
    
    var cellHeight: CGFloat {
        get {
            var height: CGFloat = 45.0
            
            if (self.topLeftString.count > 0) {
                height += 20.0
            }
            if (self.topCenterString.count > 0) {
                height += 20.0
            }
            if (self.topRightString.count > 0) {
                height += 20.0
            }
            if (self.bottomLeftString.count > 0) {
                height += 20.0
            }
            if (self.bottomCenterString.count > 0) {
                height += 20.0
            }
            if (self.bottomRightString.count > 0) {
                height += 20.0
            }
            
            return height
        }
    }
    
    var hasVaild: Bool {
        get {
            for string in [self.topLeftString, self.topCenterString, self.topRightString,
                           self.bottomLeftString, self.bottomCenterString, self.bottomRightString] {
                if (!string.isEmpty) {
                    return true
                }
            }
            
            return false
        }
    }
    
    override init() {
        super.init()
        
        self.textFont = .font(name: "Helvetica", size: 10)
        self.textColor = .color(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    }
    
    required init?(coder: NSCoder) {
        super.init()
        type = KMHeaderFooterObjectType.init(rawValue: coder.decodeInteger(forKey: "type"))!
        pageRangeType = KMWatermarkeModelPageRangeType.init(rawValue: coder.decodeInteger(forKey: "pageChoice"))!
        leftMargin = coder.decodeInteger(forKey: "leftMargin")
        rightMargin = coder.decodeInteger(forKey: "rightMargin")
        bottomMargin = coder.decodeInteger(forKey: "bottomMargin")
        topMargin = coder.decodeInteger(forKey: "topMargin")
        topLeftString = coder.decodeObject(forKey: "topLeftString") as? String ?? ""
        topCenterString = coder.decodeObject(forKey: "topCenterString") as? String ?? ""
        topRightString = coder.decodeObject(forKey: "topRightString") as? String ?? ""
        bottomLeftString = coder.decodeObject(forKey: "bottomLeftString") as? String ?? ""
        bottomCenterString = coder.decodeObject(forKey: "bottomCenterString") as? String ?? ""
        bottomRightString = coder.decodeObject(forKey: "bottomRightString") as? String ?? ""
        startString = coder.decodeObject(forKey: "startString") as? String ?? ""
        fontSize = coder.decodeFloat(forKey: "fontSize")
        tempTextColor = coder.decodeObject(forKey: "textColor") as? NSColor ?? NSColor.black
        id = coder.decodeObject(forKey: "headerFooterID") as? String ?? ""
        pageRangeString = coder.decodeObject(forKey: "pagesString") as? String ?? ""
//        hasHeader = coder.decodeBool(forKey: "hasHeader")
//        hasFooter = coder.decodeBool(forKey: "hasFooter")
//        cellHeight = coder.decodeFloat(forKey: "cellHeight")
//        dateFormatString = coder.decodeObject(forKey: "dateFormatString") as? String ?? "m/d"
//        pageFormatString = coder.decodeObject(forKey: "pageFormatString") as? String ?? "1"
//        isBates = coder.decodeBool(forKey: "isBates")
//        batesPrefixString = coder.decodeObject(forKey: "batesPrefixString") as? String ?? ""
//        batesSuffixString = coder.decodeObject(forKey: "batesSuffixString") as? String ?? ""
//        batesDigits = coder.decodeInteger(forKey: "batesDigits")
    }

    func encode(with coder: NSCoder) {
        coder.encode(type.rawValue, forKey: "type")
        coder.encode(pageRangeType.rawValue, forKey: "pageChoice")
        coder.encode(leftMargin, forKey: "leftMargin")
        coder.encode(rightMargin, forKey: "rightMargin")
        coder.encode(bottomMargin, forKey: "bottomMargin")
        coder.encode(topMargin, forKey: "topMargin")
        coder.encode(topLeftString, forKey: "topLeftString")
        coder.encode(topCenterString, forKey: "topCenterString")
        coder.encode(topRightString, forKey: "topRightString")
        coder.encode(bottomLeftString, forKey: "bottomLeftString")
        coder.encode(bottomCenterString, forKey: "bottomCenterString")
        coder.encode(bottomRightString, forKey: "bottomRightString")
        coder.encode(startString, forKey: "startString")
        coder.encode(fontSize, forKey: "fontSize")
        coder.encode(self.getTextColor(), forKey: "textColor")
//        coder.encode(hasHeader, forKey: "hasHeader")
//        coder.encode(hasFooter, forKey: "hasFooter")
//        coder.encode(cellHeight, forKey: "cellHeight")
        coder.encode(id, forKey: "headerFooterID")
        coder.encode(pageRangeString, forKey: "pagesString")
        
//        coder.encode(dateFormatString, forKey: "dateFormatString")
//        coder.encode(pageFormatString, forKey: "pageFormatString")
//        coder.encode(isBates, forKey: "isBates")
//        coder.encode(batesPrefixString, forKey: "batesPrefixString")
//        coder.encode(batesSuffixString, forKey: "batesSuffixString")
//        coder.encode(batesDigits, forKey: "batesDigits")
    }

//    func copy(with zone: NSZone? = nil) -> Any {
//        let obj = KMHeaderFooterObject()
//        obj.pageRangeType = pageRangeType
//        obj.leftMargin = leftMargin
//        obj.rightMargin = rightMargin
//        obj.bottomMargin = bottomMargin
//        obj.topMargin = topMargin
//        obj.topLeftString = topLeftString
//        obj.topCenterString = topCenterString
//        obj.topRightString = topRightString
//        obj.bottomLeftString = bottomLeftString
//        obj.bottomCenterString = bottomCenterString
//        obj.bottomRightString = bottomRightString
//        obj.startString = startString
//        obj.fontSize = fontSize
//        obj.textColor = textColor
////        obj.hasHeader = hasHeader
////        obj.hasFooter = hasFooter
////        obj.cellHeight = cellHeight
//        obj.id = id
//        obj.pageCount = pageCount
//        obj.pageRangeString = pageRangeString
//        
//        obj.dateFormatString = dateFormatString
//        obj.pageFormatString = pageFormatString
//        obj.isBates = isBates
//        obj.batesPrefixString = batesPrefixString
//        obj.batesSuffixString = batesSuffixString
//        obj.batesDigits = batesDigits
//        return obj
//    }
}