//
//  FakeAddView.swift
//  RealCalendar
//
//  Created by Lee on 2020/08/25.
//  Copyright © 2020 UPs. All rights reserved.
//

import UIKit

protocol FakeAddViewDelegate: AnyObject {
  func cancelDidTap()
  func settingDidTap(colorTag: Int, fakeTag: Int?, title: String?, content: String?)
}

final class FakeAddView: UIView {
  
  weak var delegate: FakeAddViewDelegate?
  
  private var colorTag = 0 {
    didSet { setColorCheckmark(tag: colorTag) }
  }
  private var fakeTag: Int?
  
  private let colorDisplayLabel = UILabel()
  private var colorButtons = [UIButton]()
  private let colorStackView = UIStackView()
  
  private let fakeTagDisplayLabel = UILabel()
  private let fakeTagDropPicker = DropPicker(trailingText: "")
  
  private let titleDisplayLabel = UILabel()
  private let titleTextField = PaddingTextField()
  
  private let contentDisplayLabel = UILabel()
  private let contentTextView = UITextView()
  
  private let cancelButton = UIButton()
  private let settingButton = UIButton()

  override init(frame: CGRect) {
    super.init(frame: frame)
    
    setAttutibute()
    setConstraint()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}



// MARK: - UI

extension FakeAddView {
  private func setAttutibute() {
    self.backgroundColor = .systemBackground
    self.layer.cornerRadius = 4
    self.layer.masksToBounds = true
    
    colorDisplayLabel.text = "색"
    fakeTagDisplayLabel.text = "태그"
    titleDisplayLabel.text = "제목"
    contentDisplayLabel.text = "내용"
    [colorDisplayLabel, fakeTagDisplayLabel, titleDisplayLabel, contentDisplayLabel].forEach {
      $0.font = .boldSystemFont(ofSize: 16)
    }
    
    UIColor.colorArray.enumerated().forEach {
      let tempButton = UIButton()
      tempButton.tag = $0
      tempButton.setTitleColor(.label, for: .normal)
      tempButton.titleLabel?.font = .systemFont(ofSize: 20, weight: .medium)
      tempButton.backgroundColor = $1
      tempButton.layer.cornerRadius = 4
      tempButton.addTarget(self, action: #selector(colorButtonDidTap(_:)), for: .touchUpInside)
      colorButtons.append(tempButton)
    }
    setColorCheckmark(tag: 0)
    
    colorStackView.spacing = 8
    colorStackView.alignment = .center
    colorStackView.distribution = .fillEqually
    
    fakeTagDropPicker.delegate = self
    
    [titleTextField, contentTextView].forEach {
      $0.backgroundColor = .mySubBoard
      $0.layer.cornerRadius = 4
    }
    
    cancelButton.setTitle("닫기", for: .normal)
    cancelButton.backgroundColor = .myBoard
    
    settingButton.setTitle("설정", for: .normal)
    settingButton.backgroundColor = .mySubBoard
    
    [cancelButton, settingButton].forEach {
      $0.setTitleColor(.label, for: .normal)
      $0.addTarget(self, action: #selector(buttonDidTap(_:)), for: .touchUpInside)
    }
  }
  
  private func setConstraint() {
    let xSpace: CGFloat = 24
    let ySpace: CGFloat = 16
    let toSpace: CGFloat = 8
    
    [colorDisplayLabel, colorStackView,
     fakeTagDisplayLabel, fakeTagDropPicker,
     titleDisplayLabel, titleTextField,
     contentDisplayLabel, contentTextView,
     cancelButton, settingButton
      ].forEach {
        self.addSubview($0)
        $0.translatesAutoresizingMaskIntoConstraints = false
    }
    
    colorButtons.forEach {
      colorStackView.addArrangedSubview($0)
    }
    
    [colorDisplayLabel, colorStackView, fakeTagDisplayLabel, titleDisplayLabel, titleTextField, contentDisplayLabel, contentTextView].forEach {
      $0.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: xSpace).isActive = true
      $0.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -xSpace).isActive = true
    }
    
    [cancelButton, settingButton].forEach {
      $0.topAnchor.constraint(equalTo: contentTextView.bottomAnchor, constant: ySpace).isActive = true
      $0.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
      $0.heightAnchor.constraint(equalToConstant: 48).isActive = true
    }
    
    NSLayoutConstraint.activate([
      colorDisplayLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: ySpace),
      
      colorStackView.topAnchor.constraint(equalTo: colorDisplayLabel.bottomAnchor, constant: toSpace),
      colorStackView.heightAnchor.constraint(equalToConstant: 32),
      
      fakeTagDisplayLabel.topAnchor.constraint(equalTo: colorStackView.bottomAnchor, constant: ySpace),
      
      fakeTagDropPicker.topAnchor.constraint(equalTo: fakeTagDisplayLabel.bottomAnchor, constant: toSpace),
      fakeTagDropPicker.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: xSpace),
      fakeTagDropPicker.widthAnchor.constraint(equalToConstant: 88),
      
      titleDisplayLabel.topAnchor.constraint(equalTo: fakeTagDisplayLabel.bottomAnchor, constant: ySpace + toSpace + 32),
      
      titleTextField.topAnchor.constraint(equalTo: titleDisplayLabel.bottomAnchor, constant: toSpace),
      
      contentDisplayLabel.topAnchor.constraint(equalTo: titleTextField.bottomAnchor, constant: ySpace),
      
      contentTextView.topAnchor.constraint(equalTo: contentDisplayLabel.bottomAnchor, constant: toSpace),
      contentTextView.heightAnchor.constraint(equalToConstant: 120),
      
      cancelButton.leadingAnchor.constraint(equalTo: self.leadingAnchor),
      
      settingButton.leadingAnchor.constraint(equalTo: cancelButton.trailingAnchor),
      settingButton.trailingAnchor.constraint(equalTo: self.trailingAnchor),
      settingButton.widthAnchor.constraint(equalTo: cancelButton.widthAnchor)
    ])
  }
  
  private func setColorCheckmark(tag: Int) {
    colorButtons.enumerated().forEach { (index, button) in
      let text = index == tag ? "✓" : nil
      button.setTitle(text, for: .normal)
    }
  }
}



// MARK: - Action

extension FakeAddView {
  @objc private func buttonDidTap(_ sender: UIButton) {
    sender == cancelButton ?
      delegate?.cancelDidTap() :
      delegate?.settingDidTap(colorTag: colorTag, fakeTag: fakeTag, title: titleTextField.text, content: contentTextView.text)
  }
  
  @objc private func colorButtonDidTap(_ sender: UIButton) {
    colorTag = sender.tag
  }
}



// MARK: - DropPickerDelegate

extension FakeAddView: DropPickerDelegate {
  func setElement(_ view: UIView) -> [Int] {
    var tempElement = Array<Int>(0...99)
    
    let removeElement = FakeManager.list.map { $0.fakeTag }
    removeElement.reversed().forEach {
      tempElement.remove(at: $0)
    }
    
    return tempElement
  }
  
  func viewDidTap(_ view: UIView) {}
  
  func didSelect(_ view: UIView, _ element: Int) {
    fakeTag = element
  }
}
