//
//  MessagesViewController.swift
//  iMessage
//
//  Created by Timothy Barnard on 24/09/2017.
//  Copyright © 2017 Timothy Barnard. All rights reserved.
//

import UIKit
import Messages

class MessagesViewController: MSMessagesAppViewController {

    @IBOutlet weak var tableView: UITableView!
    
    var savedConversation: MSConversation?
    var timestables : [Timetable] = [Timetable]() {
        didSet {
            tableView.reloadData()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        
        let today = Date()
        let dayIndex = today.weekDayIndex()
        let database = Database()
        self.timestables = database.getDayTimetable(dayNo: dayIndex)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Conversation Handling
    
    override func willBecomeActive(with conversation: MSConversation) {
        // Called when the extension is about to move from the inactive to active state.
        // This will happen when the extension is about to present UI.
        
        // Use this method to configure the extension and restore previously stored state.
        
        savedConversation = conversation
    }
    
    override func didResignActive(with conversation: MSConversation) {
        // Called when the extension is about to move from the active to inactive state.
        // This will happen when the user dissmises the extension, changes to a different
        // conversation or quits Messages.
        
        // Use this method to release shared resources, save user data, invalidate timers,
        // and store enough state information to restore your extension to its current state
        // in case it is terminated later.
    }
   
    override func didReceive(_ message: MSMessage, conversation: MSConversation) {
        // Called when a message arrives that was generated by another instance of this
        // extension on a remote device.
        
        // Use this method to trigger UI updates in response to the message.
    }
    
    override func didStartSending(_ message: MSMessage, conversation: MSConversation) {
        // Called when the user taps the send button.
    }
    
    override func didCancelSending(_ message: MSMessage, conversation: MSConversation) {
        // Called when the user deletes the message without sending it.
    
        // Use this to clean up state related to the deleted message.
    }
    
    override func willTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
        // Called before the extension transitions to a new presentation style.
    
        // Use this method to prepare for the change in presentation style.
    }
    
    override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
        // Called after the extension transitions to a new presentation style.
    
        // Use this method to finalize any behaviors associated with the change in presentation style.
    }

}

extension MessagesViewController: UITableViewDelegate, UITableViewDataSource {
    
    // MARK: - Table view data source
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.timestables.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "classIdentifier", for: indexPath) as! IMessageCell
        
        let cellTimetable = self.timestables[indexPath.row]
        
        cell.className.text = cellTimetable.name
        //cell.backgroundColor = UIColor.white
        cell.classLecture.text = " "+cellTimetable.lecture
        cell.classLocation.text = cellTimetable.room
        cell.classTime.text = " "+cellTimetable.timeStart.convertToCurrentTimeFormat() + "-"+cellTimetable.timeEnd.convertToCurrentTimeFormat()
        cell.classGroups.text = cellTimetable.groups
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let timetable = self.timestables[indexPath.row]
        let message = MSMessage()
        let layout = MSMessageTemplateLayout()
        layout.caption = timetable.name
        layout.subcaption = timetable.timeStart
        layout.trailingSubcaption = timetable.room
        message.layout = layout
        savedConversation?.insert(message, completionHandler: nil)
        
    }
}
