//
//  KMBaseTextFieldModel.swift
//  PDF Master
//
//  Created by lizhe on 2022/12/23.
//

import Cocoa

enum KMBaseTextFieldUnit {
    case mm // 毫米
    case cm // 厘米
    case inch // 英寸
}

enum KMBaseTextFieldInputType {
    case none
    case int //整形
    case float //带一位小数点
    case float_2 //带二位小数点
    case percent //带百分号
}

class KMBaseTextFieldModel: NSObject {
    
    var value: String = ""
    var stringValue: String {
        return self.valueConversion(value: self.value, type: self.inputType)
    }
    
    var placeholderString: String = ""
    var isEnabled: Bool?
    var minLen: Int? = 0
    var maxLen: Int = Int.max
    var minValue: Int = Int.min
    var maxValue: Int = Int.max
    var onlyNumber: Bool = true
    var inputType: KMBaseTextFieldInputType = .none
    var specialChart: String = ""
    var isCanNull: Bool = false //问题可以为 nil 字符串
}


extension  KMBaseTextFieldModel {
    /**
     @abstract 单位转换
     @value 转换值float  (方便转换)  像素
     @unit 转换类型
     */
    func unitConversion(value: Float, unit: KMBaseTextFieldUnit) -> Float {
        var scale: Float = 1.0
        var result = value
        
        switch unit {
        case .cm:
            scale = 595.0 / 21.0
        case .mm:
            scale = 595.0 / 210.0
        case .inch:
            scale = (595.0 / 21.0) * 2.54
        }
            
        result = result * scale
        return result
    }
    
    /**
     @abstract 数值转换
     @value 转换值float  (方便转换)  参数
     @type 转换类型
     */
    func valueConversion(value: String, type: KMBaseTextFieldInputType) -> String {
        var string: NSString
        
        switch type {
        case .int:
            string = NSString(format: "%.0f", value)
        case .float:
            string = NSString(format: "%.1f", value)
        case .float_2:
            string = NSString(format: "%.2f", value)
        case .percent:
            string = NSString(format: "%.0f%", value)
        case .none:
            string = NSString(format: "%@", value)
        }

        return string as String
    }
}