//
//  HelpViewController.swift
//  iPOS
//
//  Created by bbc on 06/06/17.
//  Copyright © 2017 BBC. All rights reserved.
//

import Foundation
import UIKit
import MessageUI

class HelpViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    var tableRows = [Util.getString("Report a problem"), Util.getString("About us"), Util.getString("About app")]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNavigationBarItem()
        self.title = Util.getString("Help")
        setTableViewProperties()
    }
    
    func setTableViewProperties(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.separatorStyle = .none
        self.tableView.isScrollEnabled = false
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        if let indexPath = tableView.indexPathForSelectedRow {
            tableView.deselectRow(at: indexPath, animated: animated)
        }
    }
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableRows.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! HelpCustomCell
        cell.title.text = self.tableRows[indexPath.row]
        
        switch indexPath.row {
        case 0:
            cell.defaultImage.image = UIImage.init(named: "error.png")
        case 1:
            cell.defaultImage.image = UIImage.init(named: "Info.png")
        case 2:
            cell.defaultImage.image = UIImage.init(named: "logo.jpg")
        default:
            break
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            self.sendAction()
        case 1:
            goToAboutUsViewController()
        case 2:
            goToAboutAppViewController()
        default:
            break
        }
    }
    
    func goToAboutUsViewController(){
        let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "aboutUsViewController") as! AboutUsViewController
        self.navigationController?.pushViewController(nextViewController, animated: true)
    }
    
    func goToAboutAppViewController(){
        let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "aboutAppViewController") as! AboutAppViewController
        self.navigationController?.pushViewController(nextViewController, animated: true)
    }
}

extension HelpViewController:  MFMailComposeViewControllerDelegate, UINavigationControllerDelegate{
    func sendAction() {
        if MFMailComposeViewController.canSendMail(){
            self.sendMail()
        }
        else{
            Mail.showSendMailErrorAlert(self)
        }
    }
    
    func sendMail(){
        let mail = Mail()
        mail.viewController = self
        mail.email = "info@bbctech.it"
        mail.subject = "Reporting the problem"
        mail.message = ""
        mail.isHTMLMessage = false
        mail.composeMailImage()
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
    }
}

extension HelpViewController : SlideMenuControllerDelegate {
    
    func leftWillOpen() {
    }
    
    func leftDidOpen() {
    }
    
    func leftWillClose() {
    }
    
    func leftDidClose() {
    }
    
    func rightWillOpen() {
    }
    
    func rightDidOpen() {
    }
    
    func rightWillClose() {
    }
    
    func rightDidClose() {
    }
}

