//
//  NSString+Add.swift
//  htcm_swift
//
//  Created by soldoros on 2019/1/10.
//  Copyright © 2019 soldoros. All rights reserved.
//

import Foundation
import UIKit

extension NSString{

    
    //整型转换成NSString
    class func makeStringInt(number:NSInteger)->NSString{
        return  NSString.init(format: "%d", number) as NSString
    }
    
    
    /// 将字符串每间隔number个就生成一个空格再返回
    ///
    /// - Parameters:
    ///   - number: 传入间隔的数量
    /// - Returns: 返回处理后的字符串
    func getStringWithInterval(number:NSInteger) -> NSString{
        
        let num:NSNumber = NSNumber.init(value: self.integerValue)
        let formatter:NumberFormatter = NumberFormatter()
        formatter.usesGroupingSeparator = true
        formatter.groupingSize = number
        formatter.groupingSeparator = " "
        
        return formatter.string(from: num)! as NSString
    }

    
    
    /// 获取文本的尺寸
    ///
    /// - Parameters:
    ///   - text: 文本
    ///   - font: 字体
    ///   - maxSize: 最大宽高
    /// - Returns: 返回尺寸
    class func getTextSize(text : String , font : UIFont , maxSize : CGSize) -> CGSize{
        return text.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSAttributedString.Key.font : font], context: nil).size
    }

    
    
    /// 设置字符串的高度
    ///
    /// - Parameters:
    ///   - string: 传入字符串
    ///   - height: 传入行间距
    /// - Returns: 返回可变字符串
    class func getAttstringWithHeight(string:String,height:CGFloat)->NSMutableAttributedString{
    
        let attString = NSMutableAttributedString(string:string)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = height
        attString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, string.count))
        return attString
        
    }
    
    
    /// 设置字符串的颜色
    ///
    /// - Parameters:
    ///   - string: 传入字符串
    ///   - color: 传入颜色
    /// - Returns: 返回可变字符串
    class func getAttstringWithColor(string:String,color:UIColor,rang:NSRange)->NSMutableAttributedString{
        
        let attString = NSMutableAttributedString.init(string: string)
        attString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: rang)
        return attString
    }
    
    /// 设置字符串的大小
    ///
    /// - Parameters:
    ///   - string: 传入字符串
    ///   - font: 传入字体
    /// - Returns: 返回可变字符串
    class func getAttstringWithFont(string:String,font:UIFont,rang:NSRange)->NSMutableAttributedString{
        
        let attString = NSMutableAttributedString.init(string: string)
        attString.addAttribute(NSAttributedString.Key.font, value: font,range: rang)
        return attString
    }
    
    
    
    
    /// 将字符串处理成间隔空格的字符串
    ///
    /// - Parameters:
    ///   - string: 传入完整的字符串
    ///   - space: 每间隔space个字符就增加一个空格
    /// - Returns: 返回处理后的字符串
    class func getBlankSpaceString(string:NSString, space:NSInteger)->NSString{
        
        let number = NSNumber.init(floatLiteral: string.doubleValue)
        let formatter:NumberFormatter = NumberFormatter()
        formatter.usesGroupingSeparator = true
        formatter.groupingSize = space
        formatter.groupingSeparator = " "
        let str = formatter.string(from: number)
        return str! as NSString
    }
    
    
    
    /// 将json字符串转换成字典类型 并返回
    ///
    /// - Parameter string: 传入json字符串
    /// - Returns: 返回字典类型
    class func makeDictionaryWithJson(string:String) ->NSDictionary {
        
        let jsonData:Data = string.data(using: .utf8)!
        
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if dict != nil {
            return dict as! NSDictionary
        }
        return NSDictionary()
    }
    
    
    
    /// 将json字符串转换成数组类型 并返回
    ///
    /// - Parameter string: 传入json字符串
    /// - Returns: 返回数组
    class func makeArrayWithJson(string:String) ->NSArray {
        
        let jsonData:Data = string.data(using: .utf8)!
        
        let array = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if array != nil {
            return array as! NSArray
        }
        return []
    }
    
    
    
    
    /// 将字典转换成json字符串 并返回
    ///
    /// - Parameter dictionary: 传入字典
    /// - Returns: 返回json字符串
    class func makeJsonWithDictionary(dictionary:NSDictionary) ->String {
        
        if (!JSONSerialization.isValidJSONObject(dictionary)) {
            print("无法解析出JSONString")
            return ""
        }
        let data : NSData! = try? JSONSerialization.data(withJSONObject: dictionary, options: []) as NSData
        let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
        return JSONString! as String
    }
    
    
    /// 将数组转换成json字符串 并返回
    ///
    /// - Parameter array: 传入数组
    /// - Returns: 返回json字符串
    class func makeJsonWithArray(array:NSArray) ->String {
        
        if (!JSONSerialization.isValidJSONObject(array)) {
            print("无法解析出JSONString")
            return ""
        }
        
        let data : NSData! = try? JSONSerialization.data(withJSONObject: array, options: []) as NSData
        let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
        return JSONString! as String
    }
    
    
    /// 正则判断是不是英文字母
    class func yingwenzimu(str:NSString) -> Bool{
        
        let regex:NSString = "^[A-Za-z]+$"
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,regex)
        return predicate.evaluate(with: str)
    }
    
    /// 正则判断用户名
    class func yonghuming(str:NSString) -> Bool{
        
        let regex:NSString = "^[a-zA-Z]\\w{5,15}$"
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,regex)
        return predicate.evaluate(with: str)
    }
    
    /// 正则判断手机号
    class func shoujihao(str:NSString) -> Bool{
        
        let regex:NSString = "^1+[345789]+\\d{9}"
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,regex)
        return predicate.evaluate(with: str)
    }
    
    /// 正则判断验证码
    class func yanzhengma(str:NSString) -> Bool{
        
        let regex:NSString = "^\\d{6}$"
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,regex)
        return predicate.evaluate(with: str)
    }
    
    /// 正则判断密码
    class func mima(str:NSString) -> Bool{
        
        let regex:NSString = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32}$"
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,regex)
        return predicate.evaluate(with: str)
    }
    
    /// 正则判断身份证号
    class func shenfenzheng(str:NSString) -> Bool{
        
        let regex:NSString = "\\d{14}[[0-9],0-9xX]"
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,regex)
        return predicate.evaluate(with: str)
    }
    
    
    /// 正则判断邮箱
    class func youxiang(str:NSString) -> Bool{
        
        let regex:NSString = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,regex)
        return predicate.evaluate(with: str)
    }
    
    
    
}
