//
//  UIViewExt.swift
//  ExtensionKit
//
//  Created by FFIB on 2017/9/13.
//  Copyright © 2017年 FFIB. All rights reserved.
//

import UIKit

/// MARK: Frame Extension
extension UIView {
    public var x: CGFloat {
        get {
           return frame.origin.x
        }
        set {
            frame = CGRect(x: newValue, y: y, width: width, height: height)
        }
    }
    
    public var y: CGFloat {
        get {
            return frame.origin.y
        }
        set {
            frame = CGRect(x: x, y: newValue, width: width, height: height)
        }
    }
    
    public var width: CGFloat {
        get {
            return frame.width
        }
        set {
            frame = CGRect(x: x, y: y, width: newValue, height: height)
        }
    }
    
    public var height: CGFloat {
        get {
            return frame.height
        }
        set {
            frame = CGRect(x: x, y: y, width: width, height: newValue)
        }
    }
    
    public var size: CGSize {
        get {
           return frame.size
        }
        set {
            frame = CGRect(x: x, y: y, width: newValue.width, height: newValue.height)
        }
    }
    
    public var origin: CGPoint {
        get {
            return frame.origin
        }
        set {
            frame = CGRect(x: origin.x, y: origin.y, width: width, height: height)
        }
    }
}

/// MARK: Layer Extension
extension UIView {
    @IBInspectable public  var borderWidth: CGFloat {
        get {
            return self.layer.borderWidth
        }
        set {
            self.layer.borderWidth = newValue
            self.layer.borderColor = UIColor.lightGray.cgColor
        }
        
    }
    @IBInspectable public  var cornerRadius: CGFloat {
        get {
            return self.layer.cornerRadius
        }
        set {
            self.layer.masksToBounds = true
            self.layer.cornerRadius = newValue
        }
    }
    @IBInspectable public  var borderColor: UIColor? {
        get {
            guard let color = self.layer.borderColor else {
                return nil
            }
            return UIColor(cgColor: color)
        }
        set {
            if let color = newValue {
                self.layer.borderColor = color.cgColor
            }
        }
    }
    
    @IBInspectable
    public var shadowColor: UIColor? {
        get {
            guard let color = layer.shadowColor else {
                return nil
            }
            return UIColor(cgColor: color)
        }
        set {
            if let color = newValue {
                layer.shadowColor = color.cgColor
            }
            
        }
    }
    
    @IBInspectable
    public var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }
    
    @IBInspectable
    public var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }
}

/// MARK: custom attribute
extension UIView {
    public var screenshot: UIImage? {
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, UIScreen.main.scale)
        defer {
            UIGraphicsEndImageContext()
        }
        guard let content = UIGraphicsGetCurrentContext() else {
            return nil
        }
        layer.render(in: content)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
    
    public var visiable: Bool {
        return !isHidden && alpha > 0 && window != nil
    }
}

extension UIView {
    func getSuperViewController() -> UIViewController? {
        var nr = next
        while let r = nr {
            if let vc = r as? UIViewController { return vc }
            nr = r.next
        }
        
        return nil
    }
}