//
//  TextFieldShadow.swift
//  WOKA
//
//  Created by MacBook Pro on 29/04/24.
//

import UIKit

class TextFieldShadow: UITextField {

    lazy var innerShadow: CALayer = {
        let innerShadow = CALayer()
        layer.addSublayer(innerShadow)
        return innerShadow
    }()

    override func layoutSubviews() {
        super.layoutSubviews()
        applyDesign()
    }

    func applyDesign() {
        innerShadow.frame = bounds

        // Shadow path (1pt ring around bounds)
        let radius = self.frame.size.height/2
        let path = UIBezierPath(roundedRect: innerShadow.bounds.insetBy(dx: -1, dy:-1), cornerRadius:radius)
        let cutout = UIBezierPath(roundedRect: innerShadow.bounds, cornerRadius:radius).reversing()


        path.append(cutout)
        innerShadow.shadowPath = path.cgPath
        innerShadow.masksToBounds = true
        // Shadow properties
        innerShadow.shadowColor = UIColor.black.cgColor
        innerShadow.shadowOffset = CGSize(width: 0, height: 1.2)
        innerShadow.shadowOpacity = 0.8
        innerShadow.shadowRadius = 2
        innerShadow.cornerRadius = self.frame.size.height/2
    }
}

extension UIView {
    func applyInnerShadow(radius : CGFloat) {
        let innerShadow = CALayer()
        layer.addSublayer(innerShadow)
        
        // Inner shadow setup
        innerShadow.frame = bounds
        
        // Shadow path (1pt ring around bounds)
        let radius = radius
        let path = UIBezierPath(roundedRect: innerShadow.bounds.insetBy(dx: -1, dy: -1), cornerRadius: radius)
        let cutout = UIBezierPath(roundedRect: innerShadow.bounds, cornerRadius: radius).reversing()
        
        path.append(cutout)
        innerShadow.shadowPath = path.cgPath
        innerShadow.masksToBounds = true
        
        // Shadow properties
        innerShadow.shadowColor = UIColor.black.cgColor
        innerShadow.shadowOffset = CGSize(width: 0, height: 1.2)
        innerShadow.shadowOpacity = 0.8
        innerShadow.shadowRadius = 2
        innerShadow.cornerRadius = radius
    }
}

