//
//  NotificationManager.swift
//  LocketWidget
//
//  Created by P21-0013 on 02/08/23.
//

import Foundation
import UIKit


class NotificatioManager : NSObject {
    
    static let shared = NotificatioManager()
    
    func redirectToSpecificVC(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
            if APPDEL.isNotifyRedirection{
                let currentVc = UIApplication.shared.topViewController()
                if !(currentVc is AddFriendViewController){
                    APPDEL.setNotifyFlagFalse()
                    if currentVc is HistoryViewController && APPDEL.dictNotificationUserInfo["type"] as? String ?? "" == PushNotification.img_received{
                        if let vc = currentVc as? HistoryViewController {
                            _ = vc.view
                            vc.updateHistoryList(notification: Notification.init(name: Notification.Name.init("")))
                        }
                    }else{
                        currentVc?.dismiss(animated: true, completion: {
                            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                                if  APPDEL.dictNotificationUserInfo["type"] as? String ?? "" == PushNotification.accept{
                                    if (UIApplication.shared.topViewController() is AddFriendViewController){
                                        NotificationCenter.default.post(name: Notification.Name.UpdateFriendData, object: nil, userInfo: [PushNotification.notificationType:  PushNotification.accept as Any])
                                    } else{
                                        ScreenManager.shared.moveToAddFriendVC(selectedTab: 0)
                                    }
                                    
                                }else if APPDEL.dictNotificationUserInfo["type"] as? String  == PushNotification.received{
                                    ScreenManager.shared.moveToAddFriendVC(selectedTab: 1)
                                }else if APPDEL.dictNotificationUserInfo["type"] as? String  == PushNotification.img_received{
                                    ScreenManager.shared.showHistoryVC()
                                }
                                
                            }
                        })
                    }
                }
                else{
                    APPDEL.setNotifyFlagFalse()
                }
            }
        }
    }
    
}
