//
//  ScheduleManager.swift
//  RealCalendar
//
//  Created by Lee on 2020/08/21.
//  Copyright © 2020 UPs. All rights reserved.
//

import Foundation

// 메모 추가 또는 수정을 구분하기 위함
enum EditType {
  case add            // 메모 추가
  case edit(String)   // 메모 수정 - 수정해야 할 Index
}

class ScheduleManager {
  
  static var list = [ScheduleModel]() {
    didSet {
//      dump(list)
    }
  }
  
  private static let archiveURL: URL = {
    let tempAppSupportDir = FileManager
      .default
      .urls(for: .documentDirectory, in: .userDomainMask)
      .first!
    let tempArchiveURL = tempAppSupportDir
      .appendingPathComponent("ScheduleData")
      .appendingPathExtension("plist")
    return tempArchiveURL
  }()
  
  // 저장되어 있는 데이터를 읽어옴
  class func read(completion: @escaping (Bool) -> ()) {
    
    print("---------------------- [ ScheduleManager read ] ----------------------\n")
    
    DispatchQueue.main.async {
      do {
        let readData = try Data(contentsOf: archiveURL)
        list = try PropertyListDecoder().decode([ScheduleModel].self, from: readData)
        completion(true)
        
      } catch let error {
        print(error.localizedDescription)
        completion(false)
      }
    }
  }
  
  // 데이터를 추가 및 수정
  class func write(schecdule: ScheduleModel, completion: @escaping (Bool) -> ()) {
    
    print("---------------------- [ ScheduleManager write ] ----------------------\n")
    
    list.append(schecdule)
    
    save(completion: completion)
  }
  
  // 데이터 삭제
  class func remove(uuid: String, completion: @escaping (Bool) -> ()) {
    
    print("---------------------- [ ScheduleManager remove ] ----------------------\n")
    
    list.enumerated().forEach {
      guard $1.uuid == uuid else { return }
      list.remove(at: $0)
    }
    
    save(completion: completion)
  }
  
  private class func save(completion: @escaping (Bool) -> ()) {
    var temp = [ScheduleModel]()
    list.reversed().forEach {
      guard $0.type != .fake else { return }
      temp.append($0)
    }
    
    DispatchQueue.main.async {
      do {
        let encodeData = try PropertyListEncoder().encode(temp)
        try encodeData.write(to: archiveURL)
        completion(true)
        
      } catch {
        completion(false)
      }
    }
  }
}

//
//class ScheduleManager {
//  static let shared = ScheduleManager()
//  private init () {}
//
//  var list = [ScheduleModel]()
//
//
//  private let archiveURL: URL = {
//    let tempAppSupportDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
//    let tempArchiveURL = tempAppSupportDir
//      .appendingPathComponent("MemoData")
//      .appendingPathExtension("plist")
//    return tempArchiveURL
//  }()
//
//
//
//  // 저장되어 있는 데이터를 읽어옴
//  func read(completion: @escaping (Bool) -> ()) {
//
//    print("---------------------- [ ScheduleManager read ] ----------------------\n")
//
//    DispatchQueue.global().async { [weak self] in
//      guard let self = self else { return }
//
//      do {
//        let readData = try Data(contentsOf: self.archiveURL)
//        self.list = try PropertyListDecoder().decode([ScheduleModel].self, from: readData)
//        completion(true)
//
//      } catch {
//        completion(false)
//      }
//    }
//  }
//
//
//
//  // 데이터를 추가 및 수정
//  func write(type: ScheduleEditType, schecdule: ScheduleModel, completion: @escaping (Bool) -> ()) {
//
//    print("---------------------- [ MemoManager write ] ----------------------\n")
//
//
//    switch type {
//    case .add:
//      list.append(schecdule)
//
//    case .edit(let index):
//      list.remove(at: index)
//      list.insert(schecdule, at: index)
//    }
//
//    DispatchQueue.global().async { [weak self] in
//      guard let self = self else { return }
//
//      do {
//        let encodeData = try PropertyListEncoder().encode(self.list)
//        try encodeData.write(to: self.archiveURL)
//        completion(true)
//
//      } catch {
//        completion(false)
//      }
//    }
//  }
//
//
//
//  // 데이터 삭제
//  func delete(index: Int, completion: @escaping (Bool) -> ()) {
//
//    print("---------------------- [ MemoManager delete ] ----------------------\n")
//
//    list.remove(at: index)
//
//    DispatchQueue.global().async { [weak self] in
//      guard let self = self else { return }
//
//      do {
//        let encodeData = try PropertyListEncoder().encode(self.list)
//        try encodeData.write(to: self.archiveURL)
//        completion(true)
//
//      } catch {
//        completion(false)
//      }
//    }
//  }
//}
