//  Created by dasdom on 30.04.16.
//  Copyright © 2016 dasdom. All rights reserved.
//

import Foundation

struct CodeCreator {
  
  var fileRepresentations: [FileRepresentation] = []
  var author = "Author"
  var company = "Company"
  
  mutating func generateFileRepresentations(from data: Data) {
    
    let parser = XMLParser(data: data)
    let parserDelegate = StoryboardXMLParserDelegate()
    parser.delegate = parserDelegate
    
    parser.parse()
    
    fileRepresentations = parserDelegate.fileRepresentations
  }
  
  func topComment() -> String {
    
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .short
    dateFormatter.timeStyle = .none
    
    var topComment = "//\n"
    topComment += "//  Created by \(author) on \(dateFormatter.string(from: Date())).\n"
    topComment += "//  Copyright (c) 2018 \(company). All rights reserved.\n"
    topComment += "//\n"
    topComment += "//  Generated by Storyboard2Code\n\n"
    return topComment
  }
  
  func swiftCodeStrings() -> [String: String] {
    
    var outputDict: [String: String] = [:]
    
    for fileRepresentation in fileRepresentations {
      outputDict[fileRepresentation.mainView.userLabel.capitalizeFirst] = topComment() + fileRepresentation.swiftCodeString
    }
    
    return outputDict
  }
  
  func objCImplementationCodeStrings() -> [String: String] {
    var outputDict: [String: String] = [:]
    
    for fileRepresentation in fileRepresentations {
      let key = "\(fileRepresentation.mainView.userLabel.capitalizeFirst).m"
      outputDict[key] = topComment() + fileRepresentation.objCImplementationCode()
    }
    
    return outputDict
  }
  
  func objCHeaderCodeStrings() -> [String: String] {
    var outputDict: [String: String] = [:]
    
    for fileRepresentation in fileRepresentations {
      let key = "\(fileRepresentation.mainView.userLabel.capitalizeFirst).h"
      outputDict[key] = topComment() + fileRepresentation.objCHeaderCode()
    }
    
    return outputDict
  }
}
