//
//  InsetsTextFiled.swift
//  sdk-demo-ios
//
//  Created by Smaato Inc on 10.07.18.
//  Copyright © 2018 Smaato Inc. All rights reserved.
//

import UIKit

class InsetTextFiled: UITextField {
    
    @IBInspectable var top: CGFloat {
        get {
            return topInset
        }
        set {
            topInset = newValue
        }
    }
    
    @IBInspectable var bottom: CGFloat {
        get {
            return bottomInset
        }
        set {
            bottomInset = newValue
        }
    }
    
    @IBInspectable var left: CGFloat {
        get {
            return leftInset
        }
        set {
            leftInset = newValue
        }
    }
    
    @IBInspectable var right: CGFloat {
        get {
            return rightInset
        }
        set {
            rightInset = newValue
        }
    }
    
    fileprivate var topInset = CGFloat(0)
    fileprivate var bottomInset = CGFloat(0)
    fileprivate var leftInset = CGFloat(0)
    fileprivate var rightInset = CGFloat(0)
    
    
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + leftInset,
                      y: bounds.origin.y + topInset,
                      width: bounds.size.width - leftInset - rightInset,
                      height: bounds.size.height - topInset - bottomInset)
    }
    
    
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + leftInset,
                      y: bounds.origin.y + topInset,
                      width: bounds.size.width - leftInset - rightInset,
                      height: bounds.size.height - topInset - bottomInset)
    }
    
    
    override public var intrinsicContentSize: CGSize {
        var intrinsicSuperViewContentSize = super.intrinsicContentSize
        intrinsicSuperViewContentSize.height += topInset + bottomInset
        intrinsicSuperViewContentSize.width += leftInset + rightInset
        return intrinsicSuperViewContentSize
    }
    
}
