//
//  UIView.swift
//  Astro
//
//  Created by Jeremy Wiebe on 2015-06-03.
//  Copyright (c) 2015 Mobify Research & Development Inc. All rights reserved.
//

import Foundation

extension UIView {
    @objc func encodeState() -> Data {
        let state = NSMutableData()
        let archiver = NSKeyedArchiver(forWritingWith: state)
        self.encodeRestorableState(with: archiver)
        archiver.finishEncoding()
        return state as Data
    }

    @objc func decodeState(_ state: Data) {
        let unarchiver = NSKeyedUnarchiver(forReadingWith: state as Data)
        self.decodeRestorableState(with: unarchiver)
    }

    // Recursive logging method that returns a padded printout of a view's subviews
    @objc func recursiveDescription(of view: UIView? = nil, depth: Int = 0) {
        let thisView = view ?? self
        let padding = String(repeating: " ", count: depth*2)
        print("\(padding)\(thisView)")
        for subview in thisView.subviews {
            recursiveDescription(of: subview, depth: depth + 1)
        }
    }
}
