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

import UIKit

class SettingsOneRowTableViewCell: UITableViewCell {
    enum ExpandedState {
        case hidden
        case expanded
        case collapsed
    }

	// MARK: Public Properties
    static var reuseIdentifier: String {
        return String(describing: self)
    }

	var onToggle: ( (_ isOn: Bool) -> () )?
	var style: SettingsCellStyle = .plain {
		didSet {
			switch style {
			case .plain:
                spinnerImageView?.isHidden = true
                spinnerImageView?.stopAnimating()
				toggleSwitch?.isHidden = true
				alertImageView?.isHidden = true
				addImageView?.isHidden = true
				rightInsetConstraint?.constant = 20.0
				leftInsetConstraint?.constant = 20.0
			case .toggle:
                spinnerImageView?.isHidden = true
                spinnerImageView?.stopAnimating()
				toggleSwitch?.isHidden = false
				alertImageView?.isHidden = true
				addImageView?.isHidden = true
				rightInsetConstraint?.constant = 80.0
				leftInsetConstraint?.constant = 20.0
			case .alert:
                spinnerImageView?.isHidden = true
                spinnerImageView?.stopAnimating()
                toggleSwitch?.isHidden = true
                alertImageView?.isHidden = false
                addImageView?.isHidden = true
                rightInsetConstraint?.constant = 50.0
                leftInsetConstraint?.constant = 20.0
            case .add,
                 .remove,
                 .unremovable:
                if style == .add {
                    //addImageView?.tintColor = UIColor(red: 27/255.0, green: 170/255.0, blue: 207/255.0, alpha: 1.0)
                    addImageView?.tintColor = UIColor.green
                    if #available(iOS 13.0, *) {
                        addImageView?.image = UIImage(systemName: "plus.circle")
                    } else {
                        addImageView?.image = UIImage(named: "ic-add-item")
                    }
                } else if style == .remove {
                    addImageView?.tintColor = UIColor.red
                    if #available(iOS 13.0, *) {
                        addImageView?.image = UIImage(systemName: "minus.circle")
                    } else {
                        addImageView?.image = nil
                    }
                } else if style == .unremovable {
                    addImageView?.tintColor = UIColor.gray
                    if #available(iOS 13.0, *) {
                        addImageView?.image = UIImage(systemName: "circle")
                    } else {
                        addImageView?.image = nil
                    }
                }
                spinnerImageView?.isHidden = true
                spinnerImageView?.stopAnimating()
				toggleSwitch?.isHidden = true
				alertImageView?.isHidden = true
				addImageView?.isHidden = false
				rightInsetConstraint?.constant = 20.0
				leftInsetConstraint?.constant = 60.0
            case .waiting:
                spinnerImageView?.isHidden = false
                spinnerImageView?.startAnimating()
                toggleSwitch?.isHidden = true
                alertImageView?.isHidden = true
                addImageView?.isHidden = true
                rightInsetConstraint?.constant = 50.0
                leftInsetConstraint?.constant = 20.0
			}
		}
	}
	
	// MARK: UI Outlets
	@IBOutlet weak var titleLabel: UILabel?
	@IBOutlet weak private var toggleSwitch: UISwitch?
	@IBOutlet weak private var alertImageView: UIImageView?
	@IBOutlet weak private var addImageView: UIImageView?
    @IBOutlet private weak var spinnerImageView: UIImageView?
	@IBOutlet weak var leftInsetConstraint: NSLayoutConstraint?
	@IBOutlet weak var rightInsetConstraint: NSLayoutConstraint?

    @IBOutlet private weak var expandedArrowLabel: UILabel?

    var expanded = ExpandedState.hidden {
        didSet {
            expandedArrowLabel?.isHidden = expanded == .hidden
            if expanded == .expanded {
                expandedArrowLabel?.transform = CGAffineTransform(rotationAngle: .pi)
            } else {
                expandedArrowLabel?.transform = .identity
            }
        }
    }
}

// MARK: - Methods
extension SettingsOneRowTableViewCell {
    override func prepareForReuse() {
        super.prepareForReuse()
        expanded = .hidden
    }
    
	override func awakeFromNib() {
		// We have to hack the switch a little to get the colours we want.
		toggleSwitch?.themeForJukebox()
		self.contentView.isAccessibilityElement = true
        spinnerImageView?.loadAnimationImages(nameStem: "ind-progress-small", count: 20)
	}
	
	@IBAction private func didToggleSwitch(_ toggle: UISwitch) {
        NSObject.cancelPreviousPerformRequests(withTarget: self)
		updateSwitch()
		if let handler = onToggle {
			handler(toggle.isOn)
		}
	}

	func setToggle(isOn: Bool) {
		toggleSwitch?.setOn(isOn, animated: false)
		updateSwitch()
	}
	
	private func updateSwitch() {
		toggleSwitch?.updateStylingForStateForJukebox()
	}
}
