//
//  NotificationVC.swift
//  Garmentoo
//
//  Created by Vijith on 08/04/22.
//

import UIKit

class NotificationVC: UIViewController {
    
    @IBOutlet weak var notificationTable: UITableView!
    var notifData = [NotificationData]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        notifData = [NotificationData(title: "Your Account", subtitle: "Get notified for account alerts", content: "Important Message Alerts", enabled: false),
                     NotificationData(title: "Your Recommendations", subtitle: "Receive recommendations based on your shopping history", content: "Personalised Notifications", enabled: false),
                     NotificationData(title: "Your Shipments", subtitle: "Find out when packages ship & arrive", content: "Shipment Notifications", enabled: false),
                     NotificationData(title: "Your Garmentoo Community", subtitle: "Receive updates from customer Q&A, Review and More", content: "Community Notifications", enabled: false),
        ]
    }
    
    @objc func notifBtnTap(sender: UIButton){
      
        
        if(notifData[sender.tag].enabled)
        {
            notifData[sender.tag].enabled = false
            DispatchQueue.main.async {
                self.notificationTable.reloadData()
            }
        }
       else
        {
            notifData[sender.tag].enabled = true
            DispatchQueue.main.async {
                self.notificationTable.reloadData()
            }
        }
    }
    
    @IBAction func onTapBackBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
}
extension NotificationVC: UITableViewDelegate,UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notifData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let  cell = tableView.dequeueReusableCell(withIdentifier: "notificationcell", for: indexPath) as! NotificationsTableViewCell
        cell.title.text = notifData[indexPath.row].title
        cell.subtitle.text = notifData[indexPath.row].subtitle
        cell.content.text = notifData[indexPath.row].content
        cell.enableDisableBtn.tag = indexPath.row
        if notifData[indexPath.row].enabled
        {
            cell.enableDisableBtn.setImage(UIImage(named: "able"), for: .normal)
        }
        else
        {
            cell.enableDisableBtn.setImage(UIImage(named: "disable"), for: .normal)
            
        }
        cell.enableDisableBtn.addTarget(self, action: #selector(notifBtnTap(sender:)), for: .touchUpInside)
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    
}
struct NotificationData {
    var title : String?
    var subtitle : String?
    var content : String?
    var enabled = false
    
}
