//
//  CustomOverlayButton.swift
//  MyTatvaCare
//
//  Created by Nirav Ramani on 05/05/25.
//

import Foundation
import UIKit

@IBDesignable
class CustomOverlayButton: UIView {

    // MARK: - Subviews
    private let backgroundView = UIView()
    private let foregroundView = UIView()
    private let overlayButton = UIButton(type: .system)
    private let textLabel = UILabel()
    private let rightImageView = UIImageView()
    private let leftImageView = UIImageView()
    private let disableView = UIView()

    private var textLabelCenterXConstraint: NSLayoutConstraint!
    private var rightImageWidthConstraint: NSLayoutConstraint!
    
    // MARK: - Button Callback
    var buttonTapCallback: (() -> Void)?

    // MARK: - Inspectables
    @IBInspectable var cornerRadius: CGFloat = 8 {
        didSet {
            backgroundView.layer.cornerRadius = cornerRadius
            foregroundView.layer.cornerRadius = cornerRadius
        }
    }

    @IBInspectable var labelText: String = "Label" {
        didSet {
            textLabel.text = labelText
        }
    }

    @IBInspectable var labelTextColor: UIColor = .white {
        didSet {
            textLabel.textColor = labelTextColor
        }
    }

    @IBInspectable var image: UIImage? = loadImage(named: "ic_arrow") {
        didSet {
            rightImageView.image = image
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }

    // MARK: - View Setup
    private func setupView() {
        clipsToBounds = true
        
        // Background View
        backgroundView.backgroundColor = CustomColor.shared.darkGreen
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        backgroundView.layer.cornerRadius = cornerRadius
        backgroundView.clipsToBounds = true
        addSubview(backgroundView)

        // Foreground View
        foregroundView.backgroundColor = CustomColor.shared.lightGreen
        foregroundView.translatesAutoresizingMaskIntoConstraints = false
        foregroundView.layer.cornerRadius = cornerRadius
        foregroundView.clipsToBounds = true
        addSubview(foregroundView)

        // Label
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.text = labelText
        textLabel.textColor = labelTextColor
        textLabel.font = FontManager.font(ofSize: 16, weight: .bold)
        foregroundView.addSubview(textLabel)

        // ImageView
        rightImageView.translatesAutoresizingMaskIntoConstraints = false
        rightImageView.contentMode = .scaleAspectFit
        rightImageView.image = loadImage(named: "ic_arrow")
        foregroundView.addSubview(rightImageView)
       
        leftImageView.translatesAutoresizingMaskIntoConstraints = false
        leftImageView.contentMode = .scaleAspectFit
        leftImageView.image = loadImage(named: "ic_back_arrow")
        leftImageView.isHidden = true
        foregroundView.addSubview(leftImageView)
        
        // Button
        overlayButton.translatesAutoresizingMaskIntoConstraints = false
        overlayButton.setTitle("", for: .normal)
        overlayButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        addSubview(overlayButton)

        // Disable View
        disableView.backgroundColor = .white
        disableView.alpha = 0.5
        disableView.translatesAutoresizingMaskIntoConstraints = false
        disableView.isHidden = true
        addSubview(disableView)

        setupConstraints()
    }

    private func setupConstraints() {
        textLabelCenterXConstraint = textLabel.centerXAnchor.constraint(equalTo: centerXAnchor, constant: -12)
        rightImageWidthConstraint = rightImageView.widthAnchor.constraint(equalToConstant: 24)
        
        NSLayoutConstraint.activate([
            // disableView
            disableView.topAnchor.constraint(equalTo: topAnchor, constant: 0),
            disableView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),
            disableView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0),
            disableView.bottomAnchor.constraint(equalTo: bottomAnchor),
            
            // Background View fills the parent
            backgroundView.topAnchor.constraint(equalTo: topAnchor, constant: 0),
            backgroundView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),
            backgroundView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0),
            backgroundView.bottomAnchor.constraint(equalTo: bottomAnchor),

            // Foreground View inset slightly
            foregroundView.topAnchor.constraint(equalTo: topAnchor, constant: 0),
            foregroundView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),
            foregroundView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0),
            foregroundView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12),

            // Label on the left
            textLabelCenterXConstraint,
            textLabel.centerYAnchor.constraint(equalTo: foregroundView.centerYAnchor),

            // Image to the right of the label
            rightImageView.leadingAnchor.constraint(equalTo: textLabel.trailingAnchor, constant: 8),
            rightImageView.centerYAnchor.constraint(equalTo: textLabel.centerYAnchor),
            rightImageWidthConstraint,
            rightImageView.heightAnchor.constraint(equalToConstant: 24),
            //rightImageView.trailingAnchor.constraint(lessThanOrEqualTo: foregroundView.trailingAnchor, constant: -12),

            leftImageView.trailingAnchor.constraint(equalTo: textLabel.leadingAnchor, constant: -8),
            leftImageView.centerYAnchor.constraint(equalTo: textLabel.centerYAnchor),
            leftImageView.widthAnchor.constraint(equalToConstant: 24),
            leftImageView.heightAnchor.constraint(equalToConstant: 24),
            
            // Button overlays entire view
            overlayButton.topAnchor.constraint(equalTo: topAnchor),
            overlayButton.leadingAnchor.constraint(equalTo: leadingAnchor),
            overlayButton.trailingAnchor.constraint(equalTo: trailingAnchor),
            overlayButton.bottomAnchor.constraint(equalTo: bottomAnchor),
        ])
      

    }

    func hideRightArrow() {
        rightImageView.isHidden = true
        textLabelCenterXConstraint.constant = 0
        rightImageWidthConstraint.constant = 0
        
        setNeedsLayout()
        layoutIfNeeded()
    }
    
    func showRightArrow() {
        rightImageView.isHidden = false
        textLabelCenterXConstraint.constant = -12
        rightImageWidthConstraint.constant = 24
        
        setNeedsLayout()
        layoutIfNeeded()
    }
    
    
    func showLeftArrow() {
        leftImageView.isHidden = false
        textLabelCenterXConstraint.constant = 12
        
        setNeedsLayout()
        layoutIfNeeded()
    }
    
    func showLeftWhatsappImage() {
        rightImageView.isHidden = true
        leftImageView.isHidden = false
        leftImageView.image = loadImage(named: "ic_whatsApp_only")
        textLabelCenterXConstraint.constant = 12
        
        setNeedsLayout()
        layoutIfNeeded()
    }
    
    func disable() {
        disableView.isHidden = false
    }
    
    func enable() {
        disableView.isHidden = true
    }
    
    func setBorderOnly() {
        foregroundView.layer.borderWidth = 1
        foregroundView.layer.borderColor = CustomColor.shared.lightGreen.cgColor
        foregroundView.backgroundColor = .white
        
        backgroundView.layer.borderWidth = 1
        backgroundView.layer.borderColor = CustomColor.shared.lightGreen.cgColor
        backgroundView.backgroundColor = UIColor(hex: "#CBF0E0")
        labelTextColor = CustomColor.shared.lightGreen
        hideRightArrow()
    }
    
    func setRedButton() {
        foregroundView.layer.borderWidth = 0
        foregroundView.backgroundColor = UIColor(hex: "#D92D20")
        
        backgroundView.layer.borderWidth = 0
        backgroundView.backgroundColor = UIColor(hex: "#B42318")
        labelTextColor = .white
        hideRightArrow()
    }
    
    @objc private func buttonTapped() {
        buttonTapCallback?()
    }
}
