//
//  SendInvitesViewController.swift
//  Sportify (iOS)
//
//  Created by mpeshevski on 7/22/21.
//

import UIKit

class SendInvitesViewController: ViewController {
  lazy var cancelButton: UIButton = {
    let l = UIButton()
    l.snp.makeConstraints { make in
      make.height.equalTo(60)
      make.width.equalTo(150)
    }
    l.setTitleColor(Color.label.color, for: .normal)
    l.setTitle("Cancel", for: .normal)
    l.layer.cornerRadius = 30
    l.layer.borderColor = UIColor.black.cgColor
    l.layer.borderWidth = 1
    l.addTarget(self, action: #selector(onCancel), for: .touchUpInside)

    return l
  }()
  
  lazy var createButton: UIButton = {
    let l = UIButton()
    l.snp.makeConstraints { make in make.height.equalTo(60) }
    l.setTitle("Create", for: .normal)
    l.layer.cornerRadius = 30
    l.backgroundColor = Color.sportifyGreen.color
    l.addTarget(self, action: #selector(onCreate), for: .touchUpInside)
    return l
  }()
  
  lazy var buttonStack = UIStackView(arrangedSubviews: [cancelButton, createButton],
                                     axis: .horizontal,
                                     spacing: 15,
                                     distribution: .fillEqually)
  lazy var buttonContainer: UIView = {
    let l = UIView()
    l.addSubview(buttonStack)
    buttonStack.snp.makeConstraints { make in make.top.bottom.centerX.equalToSuperview() }
    return l
  }()
  
  lazy var contentStack: UIStackView = {
    let l = UIStackView(arrangedSubviews: [UIView(),
                                           buttonContainer],
                        axis: .vertical,
                        spacing: 10,
                        alignment: .fill,
                        padding: UIEdgeInsets(top: 30, left: 10, bottom: 30, right: 10))

    return l
  }()
  
  var gameEvent: GameEvent
  
  init(gameEvent: GameEvent) {
    self.gameEvent = gameEvent
    super.init(nibName: nil, bundle: nil)
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(contentStack)
    contentStack.snp.makeConstraints { make in make.edges.equalTo(view.layoutMarginsGuide) }
  }
  
  @objc func onCreate() {
    games.append(gameEvent)
    NotificationCenter.default.post(name: Notification.Name("gamesUpdated"), object: nil)
    dismiss(animated: true, completion: nil)
  }
  
  @objc func onCancel() {
    dismiss(animated: true, completion: nil)
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}
