//
//  ActionSheetView.swift
//  PaiaiUIKit
//
//  Created by FFIB on 2017/11/17.
//  Copyright © 2017年 FFIB. All rights reserved.
//

import UIKit

public final class ActionSheetView: UIView {
    public static var `default`: ActionSheetView {
        return ActionSheetView()
    }
    
    private typealias ButtonAction = ((AlertAction) -> Void)
    
    public private(set) var alertActions: [AlertAction] = []
    public private(set) var cancelAction: AlertAction? = nil
    private var _cancelItem: AlertItem?
    private var _alertItems: [AlertItem] = []
    
    private var actions: [ButtonAction] = []
    private var viewNotReady = true
    
    var title: String = ""
    var message: String = ""
    
    override public func didMoveToWindow() {
        super.didMoveToWindow()
        guard viewNotReady else { return }
        constructViewHierarchy()
        activateConstraints()
        backgroundColor = UIColor(r: 153, g: 153, b: 153)
        viewNotReady = false
    }
    
    private func constructViewHierarchy() {
        for item in _alertItems { addSubview(item) }
        
        guard let item = _cancelItem else { return }
        addSubview(item)
    }
    
    private func activateConstraints() {
        activateConstraintsCancelItem()
        activateConstraintsItems()
        activateConstraintsRootView()
    }
    
    func addAlertAction(_ action: AlertAction) {
        switch action.style {
        case .default, .custom:
            alertActions.append(action)
            _alertItems.append(action.style.item)
        case .cancel:
            cancelAction = action
            _cancelItem = action.style.item
        }
    }
}

/// MARK:
fileprivate extension ActionSheetView {
    func activateConstraintsRootView() {
        guard let v = superview else { return }
        translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            bottomAnchor.constraint(equalTo: v.bottomAnchor),
            leadingAnchor.constraint(equalTo: v.leadingAnchor),
            trailingAnchor.constraint(equalTo: v.trailingAnchor)
            ])
    }
    
    func activateConstraintsCancelItem() {
        guard let alertAction = cancelAction,
            let cancelItem = _cancelItem else { return }
        cancelItem.translatesAutoresizingMaskIntoConstraints = false
        cancelItem.setAttributedTitle(alertAction.attributedTitle, for: .normal)
        
        NSLayoutConstraint.activate([
            cancelItem.heightAnchor.constraint(equalToConstant: 44),
            cancelItem.leadingAnchor.constraint(equalTo: leadingAnchor),
            cancelItem.trailingAnchor.constraint(equalTo: trailingAnchor),
            cancelItem.bottomAnchor.constraint(equalTo: bottomAnchor)
            ])
    }
    
    func activateConstraintsItems() {
        guard !alertActions.isEmpty else { return }
        var last: UIButton? = _cancelItem
        var bottom: CGFloat = _cancelItem == nil ? 0 : -6
        for (alertAction, item) in zip(alertActions, _alertItems).reversed() {
            item.translatesAutoresizingMaskIntoConstraints = false
            item.setAttributedTitle(alertAction.attributedTitle, for: .normal)
            NSLayoutConstraint.activate([
                item.heightAnchor.constraint(equalToConstant: 44),
                item.leadingAnchor.constraint(equalTo: leadingAnchor),
                item.trailingAnchor.constraint(equalTo: trailingAnchor),
                item.bottomAnchor.constraint(equalTo: last?.topAnchor ?? bottomAnchor, constant: bottom)
                ])
            last = item
            bottom = -1
        }
        NSLayoutConstraint.activate([topAnchor.constraint(equalTo: last!.topAnchor)])
    }
}

fileprivate extension AlertAction.Style {
    var item: AlertItem {
        switch self {
        case .cancel:
            return BottomCancelItem()
        case .default:
            return BottomDefaultItem()
        case let .custom(v):
            return v
        }
    }
}