//
//  SimpleToast.swift
//  Pods
//
//  Created by DevYeom on 04/06/2019.
//

import UIKit

@available(iOS 11.0, *)
open class SimpleToast: UIView {

    // MARK: - Static Constants

    private static let ShowDuration: Double = 3.0
    private static let BackgroundColor: UIColor = UIColor.init(red: 50 / 255.0, green: 65 / 255.0, blue: 117 / 255.0, alpha: 1.0)
    private static let BeginningAlpha: CGFloat = 1.0
    private static let MessageFont: UIFont = UIFont.boldSystemFont(ofSize: 15)
    private static let MessageColor: UIColor = UIColor.white

    // MARK: - Properties

    private var currentView: UIView!
    private var toastLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    public var showDuration: Double = SimpleToast.ShowDuration
    public var message: String = ""

    // MARK: - Initialization

    init?(message: String) {
        super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

        if self.windowNotReady() {
            return nil
        }

        self.message = message
        self.setupView()
        self.setupAutoLayout()
    }

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

    // MARK: - Customizable Variables
    public var backColor: UIColor = SimpleToast.BackgroundColor {
        didSet {
            weak var weakSelf = self
            if let weakSelf = weakSelf {
                weakSelf.backgroundColor = backColor
            }
        }
    }

    public var beginningAlpha: CGFloat = SimpleToast.BeginningAlpha {
        didSet {
            weak var weakSelf = self
            if let weakSelf = weakSelf {
                weakSelf.alpha = beginningAlpha
            }
        }
    }

    public var messageFont = SimpleToast.MessageFont {
        didSet {
            weak var weakSelf = self
            if let weakSelf = weakSelf {
                weakSelf.toastLabel.font = messageFont
            }
        }
    }

    public var messageColor = SimpleToast.MessageColor {
        didSet {
            weak var weakSelf = self
            if let weakSelf = weakSelf {
                weakSelf.toastLabel.textColor = messageColor
            }
        }
    }

    // MARK: - Setup

    // Cannot use when windows is nil
    private func windowNotReady() -> Bool {
        if UIApplication.shared.windows.isEmpty {
            #if DEBUG
            print("YYBottomSheet Error ::: It can't initialize SimpleToast. Because the application has no window.")
            #endif
        }
        return UIApplication.shared.windows.isEmpty
    }

    func setupView() {
        self.currentView = UIApplication.topViewController()?.view

        // Setup View
        self.backgroundColor = self.backColor
        self.layer.cornerRadius = 10
        self.alpha = self.beginningAlpha
        self.clipsToBounds = true

        // Setup Label
        self.toastLabel.backgroundColor = UIColor.clear
        self.toastLabel.textColor = self.messageColor
        self.toastLabel.textAlignment = NSTextAlignment.center
        self.toastLabel.numberOfLines = 0
        self.toastLabel.text = self.message
        self.toastLabel.font = self.messageFont
        self.addSubview(self.toastLabel)

        self.currentView.addSubview(self)
    }

    func setupAutoLayout() {
        self.translatesAutoresizingMaskIntoConstraints = false
        self.toastLabel.translatesAutoresizingMaskIntoConstraints = false

        let safeArea = self.currentView.safeAreaLayoutGuide
        self.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: 16).isActive = true
        self.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -16).isActive = true
        self.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -16).isActive = true

        self.toastLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 16).isActive = true
        self.toastLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -16).isActive = true
        self.toastLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16).isActive = true
        self.toastLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16).isActive = true
    }

    // MARK: - SimpleToast Usage
    public func show() {
        if windowNotReady() {
            return
        }

        UIView.animate(withDuration: self.showDuration, animations: {
            self.alpha = 0.0
        }, completion: {
            (isBool) -> Void in
            self.removeFromSuperview()
        })
    }
}
