//
//  UIButton.swift


import Foundation
import UIKit


extension UIButton {
    
    func downloadImage(_ imageUrl : String?, placeholderImageName: String? = "placeholder_item",allowPlaceHolder : Bool = true, _ fadeTime : Double = 0.3) {
        
        let placeholderImage = UIImage(named: placeholderImageName!)
        if imageUrl == nil || imageUrl?.count == 0 {
            self.setBackgroundImage(placeholderImage, for: .normal)
            return
        }
        let imageUrlFinal = imageUrl?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        
        if let url = URL(string: imageUrlFinal!) {
            if allowPlaceHolder{
                self.kf.setImage(with: url, for: .normal, placeholder: placeholderImage, options: nil, progressBlock: nil) { (result) in
                    switch result {
                    case .success(let value):
                        self.setBackgroundImage(value.image.imageSizeReduce(), for: .normal)
                        break
                    case .failure(let error):
                        debugPrint(error)
                        self.setBackgroundImage(placeholderImage, for: .normal)
                        break
                    }
                }
            }else{
                self.setBackgroundImage(placeholderImage, for: .normal)
            }
        }
        else {
            self.setBackgroundImage(placeholderImage, for: .normal)
        }
    }
    
    func underline() {
        let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)
        attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: (self.titleLabel?.text!.count)!))
        self.setAttributedTitle(attributedString, for: .normal)
    }
    
    func setBorder(clr : UIColor){
        self.backgroundColor = .clear
        self.layer.cornerRadius = self.frame.size.height/2
        self.layer.borderWidth = 1.0
        self.layer.borderColor = clr.cgColor
        self.clipsToBounds = true
    }
    func roundSpecificCorder(corners: UIRectCorner, radius: CGFloat){
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
                let mask = CAShapeLayer()
                mask.path = path.cgPath
                layer.mask = mask
    }
    
    func setShadow(cornerRadious : CGFloat,shadowclr : CGColor) {
        
        
        //btnAdd.backgroundColor = UIColor(red: 171, green: 178, blue: 186, alpha: 1.0)
        // Shadow and Radius
        self.layer.shadowColor = shadowclr
        self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: cornerRadious).cgPath
        self.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
        self.layer.shadowOpacity = 0.5
        self.layer.shadowRadius = 3
        self.layer.cornerRadius = cornerRadious
        self.layer.masksToBounds = true
        self.clipsToBounds = false
    }
    
    func setButtonIcon(){
        self.imageEdgeInsets = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 4)
    }
     

}
