//
//  Heart.swift
//  MVMCoreUI
//
//  Created by Lekshmi S on 07/09/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit

@objcMembers open class Heart: Control, MFButtonProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    private var heartLayer: CAShapeLayer?
    @objc public override var isSelected: Bool {
        didSet {
            heartModel?.isActive = isSelected
            updateAccessibilityLabel()
        }
    }
    public var delegateObject: MVMCoreUIDelegateObject?
    public var heartModel: HeartModel? {
        return model as? HeartModel
    }
    var additionalData: [AnyHashable: Any]?
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------
    public var widthConstraint: NSLayoutConstraint?
    public var heightConstraint: NSLayoutConstraint?
    
    //------------------------------------------------------
    // MARK: - State Handling
    //------------------------------------------------------
    open override func draw(_ rect: CGRect) {
        //Draw the heart
        heartLayer?.removeFromSuperlayer()
        let heart = drawHeart()
        layer.addSublayer(heart)
        heartLayer = heart
    }
    
    func drawHeart() -> CAShapeLayer {
        let heart = CAShapeLayer()
        let rect = bounds
        let leftArc = rect.width * 0.4
        let rightArc = rect.height * 0.3
        let arcRadius = sqrt(leftArc*leftArc + rightArc*rightArc)/2
        let heartPath = UIBezierPath()
        //Left Hand Curve
        heartPath.addArc(withCenter: CGPoint(x: rect.width * 0.3, y: rect.height * 0.35), radius: arcRadius, startAngle: 135.degreesToRadians, endAngle: 315.degreesToRadians, clockwise: true)
        //Top Centre Dip
        heartPath.addLine(to: CGPoint(x: rect.width/2, y: rect.height * 0.2))
        //Right Hand Curve
        heartPath.addArc(withCenter: CGPoint(x: rect.width * 0.7, y: rect.height * 0.35), radius: arcRadius, startAngle: 225.degreesToRadians, endAngle: 45.degreesToRadians, clockwise: true)
        //Right Bottom Line
        heartPath.addLine(to: CGPoint(x: rect.width * 0.5, y: rect.height * 0.95))
        //Left Bottom Line
        heartPath.close()
        heart.path = heartPath.cgPath
        heart.fillColor = isSelected ? heartModel?.activeColor.cgColor : heartModel?.inActiveColor.cgColor
        heart.opacity = isEnabled ? 1.0 : 0.5
        heart.lineWidth = 1
        heart.strokeColor = isSelected ? UIColor.clear.cgColor : UIColor.mvmBlack.cgColor
        return heart
    }

    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        addTarget(self, action: #selector(tapAction), for: .touchUpInside)
        widthConstraint = widthAnchor.constraint(equalToConstant: 22)
        widthConstraint?.isActive = true
        heightConstraint = heightAnchor.constraint(equalTo: widthAnchor, multiplier: 1)
        heightConstraint?.isActive = true
        isAccessibilityElement = true
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        self.delegateObject = delegateObject
        self.additionalData = additionalData
        guard let model = model as? HeartModel else { return }
        isSelected = model.isActive
        isEnabled = model.enabled && !model.readOnly
        updateAccessibilityLabel()
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    /// Adjust accessibility label based on selection of Heart.
    func updateAccessibilityLabel() {
        accessibilityHint = isEnabled ? MVMCoreUIUtility.hardcodedString(withKey: isSelected ? "heart_unfavorite_action_hint" : "heart_favorite_action_hint") : nil
        accessibilityTraits = isEnabled ? .button : .none
        accessibilityLabel = MVMCoreUIUtility.hardcodedString(withKey: isSelected ? "heart_selected_state" : "heart_not_selected_state")
    }
    
    func tapAction() {
        guard isEnabled else { return }
        isSelected = !isSelected
        if let heartModel = heartModel {
            Button.performButtonAction(with: heartModel.action, button: self, delegateObject: delegateObject, additionalData: additionalData, sourceModel: heartModel)
        }
        setNeedsDisplay()
    }
}

extension Int {
    var degreesToRadians: CGFloat { return CGFloat(self) * .pi / 180 }
}
