//
//  MainViewController.swift
//  TodoApp
//
//  Created by Lanh Vo on 8/13/19.
//  Copyright © 2019 Lanh Vo. All rights reserved.
//

import UIKit
import RealmSwift

class MainViewController: UIViewController {
    
    @IBOutlet weak var inputSuperView: UIView!
    @IBOutlet weak var todoNameTextField: UITextField!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var filterView: UIView!
    @IBOutlet weak var toggleButton: UIButton!
    @IBOutlet weak var filterLabel: UILabel!
    @IBOutlet weak var allButton: UIButton!
    @IBOutlet weak var doneButton: UIButton!
    @IBOutlet weak var activeButton: UIButton!
    
    let realm = try! Realm()
    var todoList: Results<TodoModel>?
    var type: String = Status.ALL.rawValue
    var cellHeights: [IndexPath : CGFloat] = [:]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpUI()
        self.getData()
    }
    
    func setUpUI() {
        self.setUpNavigationBar()
        self.setUpTextField()
        self.setUpTableView()
        self.setUpFilterView()
        self.hideKeyboardWhenTappedAround()
    }
    
    func setUpNavigationBar() {
        self.navigationItem.title = "Todo App"
    }
    
    func setUpTextField() {
        self.addBorder(view: self.inputSuperView)
        self.todoNameTextField.delegate = self
    }
    
    func setUpTableView() {
        self.tableView.backgroundColor = .white
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UINib(nibName: "TodoTableViewCell", bundle: nil), forCellReuseIdentifier: "TodoTableViewCell")
        self.tableView.estimatedRowHeight = 150
    }
    
    func setUpFilterView() {
        self.filterView.addShadow(offset: CGSize(width: 0, height: -1), radius: 0.5, color: UIColor(hexString: "D8D8D8")!, opacity: 0.5)
        self.addBorder(view: self.toggleButton)
        self.addBorder(view: self.allButton)
        self.addBorder(view: self.doneButton)
        self.addBorder(view: self.activeButton)
        self.setSelected(button: self.allButton, isSelected: true)
    }
    
    func addBorder(view: UIView) {
        view.layer.borderWidth = 1.0
        view.layer.borderColor = UIColor(hexString: "CDCED2")?.cgColor
        view.layer.cornerRadius = 3
    }
    
    @IBAction func didTouchToggleButton(_ sender: Any) {
        guard let todoList = self.todoList else { return }
        todoList.forEach{ self.handleCheckAction(data: $0) }
        self.getData()
    }
    
    @IBAction func didTouchAllButton(_ sender: Any) {
        self.setSelected(button: self.allButton, isSelected: true)
        self.setSelected(button: self.doneButton, isSelected: false)
        self.setSelected(button: self.activeButton, isSelected: false)
        self.type = Status.ALL.rawValue
        self.getData()
    }
    
    @IBAction func didTouchDoneButton(_ sender: Any) {
        self.setSelected(button: self.allButton, isSelected: false)
        self.setSelected(button: self.doneButton, isSelected: true)
        self.setSelected(button: self.activeButton, isSelected: false)
        self.type = Status.DONE.rawValue
        self.getData()
    }
    
    @IBAction func didTouchActiveButton(_ sender: Any) {
        self.setSelected(button: self.allButton, isSelected: false)
        self.setSelected(button: self.doneButton, isSelected: false)
        self.setSelected(button: self.activeButton, isSelected: true)
        self.type = Status.ACTIVE.rawValue
        self.getData()
    }
    
    func setSelected(button: UIButton, isSelected: Bool) {
        if isSelected {
            button.isSelected = true
            button.backgroundColor = UIColor.appMainColor
        } else {
            button.isSelected = false
            button.backgroundColor = UIColor.white
        }
    }
}
