//
//  Number+Extension.swift
//  GSAE
//
//  Created by Mazy on 2018/5/30.
//  Copyright © 2018年 GSAE. All rights reserved.
//

import Foundation

extension Int {
    
    /// 整型转字符串 如果等于0 返回空字符串
    var countString: String {
        return self == 0 ? "0" : "\(self)"
    }
    
    var string: String {
        if self == 0 {
            return "0"
        } else {
            return "\(self)"
        }
    }
    
    var weekdayString: String {
        switch self {
        case 1:
            return "周日"
        case 2:
            return "周一"
        case 3:
            return "周二"
        case 4:
            return "周三"
        case 5:
            return "周四"
        case 6:
            return "周五"
        case 7:
            return "周六"
        default:
            return ""
        }
    }
    
    var formattedDuration: String {
        
        let m: Int = self / 60
        let s: Int = self % 60

        return String(format: "%02d:%02d", m, s)
    }
}

extension Int64 {
    
    var string: String {
        if self == 0 {
            return "0"
        } else {
            return "\(self)"
        }
    }
}


extension CGFloat {
    
    func limit(width: CGFloat, height: CGFloat) -> (width: CGFloat, height: CGFloat) {
        
        let ratio = self / (width > height ? width : height)
        
        let w = width * ratio
        let h = height * ratio
        
        return (w, h)
    }
}

extension CGFloat {
    /// SwiftRandom extension
    static func random(_ lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
        return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (upper - lower) + lower
    }
}

//extension CGFloat {
//    static func random() -> CGFloat {
//        return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
//    }
//}

extension CGFloat {
    static func getRadians(with angle: Double) -> CGFloat {
        let piValue: Double = (angle / 180) * .pi
        return CGFloat(piValue)
    }
}

extension Double {
    
    /// 浮点数转百分数
    var percentString: String {
        let f = self * 100
        let formatString = f.truncatingRemainder(dividingBy: 1) == 0 ? "%.0f%%" : "%.2f%%"
        return String(format: formatString, f)
    }
    
    /// 浮点数加转符号
    var moneyString: String {
        let i = Int(self)
        if self == Double(i) {
            return "￥\(i)"
        } else {
            return String(format: "￥%.2f", self)
        }
    }
    
    /// 浮点数保留两位小数
    var shortString: String {
        let i = Int(self)
        if self == Double(i) {
            return "\(i)"
        } else {
            return String(format: "%.2f", self)
        }
    }
    
    /// 浮点数保留八位小数
    var eightDecimalString: String {
        return String(format: "%.8f", self)
    }
    
    func withDecimalCount(_ count: Int) -> String {
//        return "\(self.floorF(places: count))"
        return String(format: "%.\(count)f", self.floorF(places: count))
    }
    
    /// 浮点数保留两位小数, 0 / 1.00 / 2.02 ....
    var short_String: String {
        let i = Int(self)
        if self == Double(i) {
            if self > 0.01 {
                return String(format: "%.2f", self)
            } else {
                return "\(i)"
            }
        } else {
            if Double(self) < 0.005 {
                return "0"
            } else {
                return String(format: "%.2f", self)
            }
        }
    }
    
    /// 大金额数字转字符串
    var plusMoneyString: String {
        let i = Int(self)
        if self == Double(i) {
            return "￥\(i)"
        } else {
            return String(format: "￥%@%.2f", self >= 0 ? "+" : "", self)
        }
    }
    
    var integerString: String {
        return String(format: "%.0f", self)
    }
}

extension Double {
    var valueWithK: String {
        if self > 1000 {
            return (self / 1000).shortString + "K"
        }
        return self.shortString
    }
}

extension TimeInterval {
    
    /// 时间间隔 转 分钟:秒 格式字符串
    var msString: String {
        let int = Int(self)
        let minute = String(format: "%.2d", int / 60)
        let second = String(format: "%.2d", int % 60)
        return "\(minute):\(second)"
    }
    
    /// 时间间隔 转 小时:分钟:秒 格式字符串
    var hmsString: String {
        
        let seconds = Int(self.truncatingRemainder(dividingBy: 60))
        let minutes = Int(self.truncatingRemainder(dividingBy: 3600) / 60)
        let hours   = Int(self / 3600)
        
        if hours >= 1 {
            return "\(String(format: "%02d", hours)):\(String(format: "%02d", minutes)):\(String(format: "%02d", seconds))"
        } else if minutes >= 1 {
            return "\(String(format: "%02d", minutes)):\(String(format: "%02d", seconds))"
        } else {
            return "\(String(format: "%02d", seconds))"
        }
    }
}


extension UInt {
    
    /// byte 字节转 字符串
    var readableStringFromBytes: String {
        
        guard self > 0 else {
            return ""
        }
        
        let kb = self / 1024
        guard kb > 0 else {
            return "\(self) B"
        }
        
        let mb = kb / 1024
        guard mb > 0 else {
            return "\(kb) KB"
        }
        
        let gb = mb / 1024
        guard gb > 0 else {
            return "\(mb) MB"
        }
        
        return "\(gb) GB"
    }   
}

extension Int {
    
    /**
     转化为字符串格式
     - returns:
     */
    func toString() -> String {
        return String(self)
    }
    
    /**
     把布尔变量转化为Int
     - returns:
     */
    init(_ value: Bool) {
        if value {
            self.init(1)
        } else {
            self.init(0)
        }
    }
    
    
    /// 转为bool型
    ///
    /// - Returns:
    func toBool() -> Bool {
        if self > 0 {
            return true
        } else {
            return false
        }
    }
}

extension NSNumber {
    /// 金额number转字符串
    ///
    /// - Parameter number: 金额number
    /// - Returns: 返回字符串
    func moneyFormatter() -> String {
        let numberFormatter = NumberFormatter()
        numberFormatter.positiveFormat = "###,###.##"
        let format = numberFormatter.string(from: self)
        return format ?? ""
    }
}

import SwiftyJSON

extension JSON {
    // solve 7.024000000000 but show 7.02399999999
    public var highAccuracyValue: Double {
        return self.stringValue.toDouble()
    }
    
    public var decimalNumber: NSDecimalNumber {
        return NSDecimalNumber(string: self.stringValue)
    }
}
