//
//  SHInputViewModel.swift
//  ShorthandMaster
//
//  Created by 张哲 on 2020/9/24.
//  Copyright © 2020 明津李. All rights reserved.
//

import UIKit

class SHInputViewModel: NSObject {
    
    // MARK: - property
//    /// 标题文字
//    var title : String = ""
//    /// 内容文字
//    var content : String = ""
//    /// 图片数组
//    var imageArray : Array = Array<Any>()
    
//    var recordModel : SHRecordModel = SHRecordModel()
    
    
    
    
    
    // MARK: - func
    // MARK: 标题文字样式
    class func titleAttributes() -> NSDictionary
    {
        let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = 5;
        
        let titleAttributes = NSMutableDictionary.init()
        titleAttributes.setValue(UIFont.init(name: SHPingFangMedium, size: 18), forKey: NSAttributedString.Key.font.rawValue)
        titleAttributes.setValue(paragraphStyle, forKey: NSAttributedString.Key.paragraphStyle.rawValue)
        titleAttributes.setValue(UIColor.init(r: 34, g: 34, b: 34), forKey: NSAttributedString.Key.foregroundColor.rawValue)
        
        return titleAttributes
    }
    
    // MARK: 内容文字样式
    class func contentAttributes() -> NSDictionary
    {
        let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = 5;
        
        let titleAttributes = NSMutableDictionary.init()
        titleAttributes.setValue(UIFont.init(name: SHPingFangRegular, size: 18), forKey: NSAttributedString.Key.font.rawValue)
        titleAttributes.setValue(paragraphStyle, forKey: NSAttributedString.Key.paragraphStyle.rawValue)
        titleAttributes.setValue(UIColor.init(r: 34, g: 34, b: 34), forKey: NSAttributedString.Key.foregroundColor.rawValue)
        
        return titleAttributes
    }

    // MARK: 获取textView中的图片, 保存到本地, 并生成数组
    class func setUpImageArrayWithTextView(textView: UITextView) -> (imagesPathArray: Array<String>, imagesIndexArray: Array<Int>)
    {
        /// 图片路径数组
        var imagesPathArray = Array<String>()
        /// 图片位置数组
        var imagesIndexArray = Array<Int>()
        
        // 创建文件夹
        /// 文件夹路径
        let subFile = Date().format("yyyyMMdd")
        let time = Date.init().timeStamp
        let filePath = DocumentPath + "/" + subFile + time
        do
        {
            try FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
        }
        catch let error
        {
            print(error)
            
            return (imagesPathArray, imagesIndexArray)
        }
        
        
        // 遍历textView文字中的NSTextAttachment
        let attributedString = textView.attributedText
        
        attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in
                        
            if attributes != nil
            {
                let imageAttachment = attributes as! NSTextAttachment
                
                // 找到图片
                if imageAttachment.image != nil
                {
                    // 将图片保存在本地
                    
                    // 把图片转成Data类型
                    let imageData : Data = (imageAttachment.image?.jpegData(compressionQuality: 1.0))!
                    
                    // 保存的本地路径
                    let imagePath = "/" + subFile + time + "/" + "image.jpeg" + imagesPathArray.count.string
                    
                    let imageUrl : URL = URL(fileURLWithPath: DocumentPath + imagePath)
                    
                    var saveSucceed : Bool = Bool(true)
                    
                    // 将图片保存到沙盒
                    do
                    {
                        try imageData.write(to: imageUrl)
                    }
                    catch let error
                    {
                        saveSucceed = false
                        print(error)
                    }
                    
                    if saveSucceed
                    {
                        // 保存成功后存储保存的路径
                        imagesPathArray.append(imagePath)
                        
                        // 存储图片位置
                        imagesIndexArray.append(range.location)
                    }
                }
            }
            
        })
        
        return (imagesPathArray, imagesIndexArray)
    }
}
