import UIKit
import Lottie

// MARK: - UIView fade animations

extension UIView {
  func fadeIn(duration: TimeInterval = 0.25) {
    self.alpha = 0.0
    self.transform = CGAffineTransform(translationX: 0, y: 10)
    UIView.animate(
      withDuration: duration,
      delay: 0,
      options: [.curveEaseOut],
      animations: {
        self.alpha = 1.0
        self.transform = .identity
      }
    )
  }

  func fadeOutAndRemove(duration: TimeInterval = 0.25) {
    UIView.animate(
      withDuration: duration,
      delay: 0,
      options: [.curveEaseIn],
      animations: {
        self.alpha = 0.0
        self.transform = CGAffineTransform(translationX: 0, y: 10)
      }
    ) { _ in
      self.removeFromSuperview()
    }
  }
}

// MARK: - UIColor hex initializer

extension UIColor {
  convenience init(hex: String) {
    var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
    hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")

    // Support 8-digit hex (RRGGBBAA)
    if hexSanitized.count == 8 {
      var rgba: UInt64 = 0
      Scanner(string: hexSanitized).scanHexInt64(&rgba)
      let r = CGFloat((rgba & 0xFF00_0000) >> 24) / 255.0
      let g = CGFloat((rgba & 0x00FF_0000) >> 16) / 255.0
      let b = CGFloat((rgba & 0x0000_FF00) >> 8) / 255.0
      let a = CGFloat(rgba & 0x0000_00FF) / 255.0
      self.init(red: r, green: g, blue: b, alpha: a)
      return
    }

    var rgb: UInt64 = 0
    Scanner(string: hexSanitized).scanHexInt64(&rgb)
    let r = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
    let g = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
    let b = CGFloat(rgb & 0x0000FF) / 255.0
    self.init(red: r, green: g, blue: b, alpha: 1.0)
  }
}

// MARK: - PAM Colors

enum PAMColors {
  static let containerBg = "#F3F4F6"
  static let titleColor = "#030712"
  static let subtitleColor = "#6B7280"
}
