//
// © 2018-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

typealias SpeakerSliderDidChangeContentViewCallback = (SpeakerSliderTableViewCellContentView) -> ()

class SpeakerSliderTableViewCellContentView: UIView {
    enum ExpandedState {
        case hidden
        case expanded
        case collapsed
    }

	@IBOutlet private var contentView: UIView!
	@IBOutlet private weak var label: UILabel!
    @IBOutlet private weak var expandedArrowLabel: UILabel!
	@IBOutlet private weak var slider: UISlider!

    var expanded = ExpandedState.hidden {
        didSet {
            expandedArrowLabel.isHidden = expanded == .hidden
            if expanded == .expanded {
				expandedArrowLabel.transform = CGAffineTransform(rotationAngle: .pi)
            } else {
				expandedArrowLabel.transform = .identity
            }
        }
    }
	
	var sliderDidChange: SpeakerSliderDidChangeContentViewCallback?
	
	var speakerName: String = "" {
		didSet {
			updateLabel()
		}
	}

    var accessibilityName: String = "" {
        didSet {
            slider.makeAccessible(accessibilityName, localizableSuffix: Accessibility.SpeakerVolumesScreen.Cell.volumeSuffix)
        }
    }

    var isTracking: Bool {
        get {
            return slider.isTracking
        }
    }
	
	override var backgroundColor: UIColor? {
		didSet {
			contentView?.backgroundColor = backgroundColor
		}
	}
	
	var volumePercent: Int {
		set {
			slider.value = Float(newValue) / 100.0
			updateLabel()
		}
		get {
			return Int(slider.value * 100)
		}
	}
	
	var enabled: Bool {
		set {
			slider.isEnabled = newValue
		}
		get {
			return slider.isEnabled
		}
	}

    var showPercent: Bool {
        didSet {
            updateLabel()
        }
    }
	
	// MARK: Initializers
	override init(frame: CGRect) {
        showPercent = true
		super.init(frame: frame)
		commonInit()
	}
	
	required init?(coder aDecoder: NSCoder) {
        showPercent = true
		super.init(coder: aDecoder)
		commonInit()
	}
	
	func commonInit() {
		Bundle.main.loadNibNamed("SpeakerSliderTableViewCellContentView",
								 owner: self,
								 options: nil)
		addSubview(contentView)
		contentView?.frame = self.bounds
		contentView?.autoresizingMask = [.flexibleHeight, .flexibleWidth]
	}
}

extension SpeakerSliderTableViewCellContentView {
	private func updateLabel() {
		let attrText = NSMutableAttributedString()
		let font = UIFont.systemFont(ofSize: 18, weight: .semibold)
		let nameAttrs: [NSAttributedString.Key : Any] = [.font : font as Any,
                                                         .foregroundColor: ColorCompat.label]
        let nameText = NSAttributedString(string: speakerName, attributes: nameAttrs)
        attrText.append(nameText)

        if showPercent {
            let hyphenText = NSAttributedString(string: " - ", attributes: nameAttrs)

            let percentAttrs: [NSAttributedString.Key : Any] = [.font : font as Any,
                                                                .foregroundColor: ColorCompat.secondaryLabel]
            let percentText = NSAttributedString(string: "\(volumePercent)%", attributes: percentAttrs)

            attrText.append(hyphenText)
            attrText.append(percentText)
        }
        label.attributedText = attrText
    }

	@IBAction private func sliderChanged(_ sender: UISlider) {
        if showPercent {
            updateLabel()
        }
		if let callback = sliderDidChange {
			callback(self)
		}
	}
}
