<commit_before>//
//  PresentationManager.swift
//  Drift
//
//  Created by <NAME> on 26/01/2016.
//  Copyright © 2016 Drift. All rights reserved.
//

import UIKit

protocol PresentationManagerDelegate:class {
    
    func campaignDidFinishWithResponse(view: CampaignView, campaign: Campaign, response: CampaignResponse)
    func messageViewDidFinish(view: CampaignView)
}


///Responsible for showing a campaign
class PresentationManager: PresentationManagerDelegate {
    
    static var sharedInstance: PresentationManager = PresentationManager()
    weak var currentShownView: CampaignView?
    
    init () {}
    
    func didRecieveCampaigns(campaigns: [Campaign]) {
        
        ///Show latest first
        let sortedCampaigns = campaigns.sort {
            
            if let d1 = $0.createdAt, d2 = $1.createdAt {
                return d1.compare(d2) == .OrderedAscending
            }else{
                return false
            }
        }
        
        var nextCampaigns = [Campaign]()
        
        if campaigns.count > 1 {
            nextCampaigns = Array(sortedCampaigns.dropFirst())
        }
        
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
         
            if let firstCampaign = sortedCampaigns.first, type = firstCampaign.messageType  {
                
                switch type {
                    
                case .Announcement:
                    self.showAnnouncementCampaign(firstCampaign, otherCampaigns: nextCampaigns)
                case .NPS:
                    self.showNPSCampaign(firstCampaign, otherCampaigns: nextCampaigns)
                case .NPSResponse:
                    ()
                }
            }
        }
    }
    
    func didRecieveNewMessages(messages: [(conversationId: Int, messages: [Message])]) {
        
        if let newMessageView = NewMessageView.fromNib() as? NewMessageView where currentShownView == nil {
            
            if let window = UIApplication.sharedApplication().keyWindow {
                currentShownView = newMessageView
                
                let currentConversation = messages.first!
                let otherConversations = messages.filter({ $0.conversationId != currentConversation.conversationId })
                newMessageView.otherConversations = otherConversations                
                newMessageView.conversation = currentConversation
                newMessageView.delegate = self
                newMessageView.showOnWindow(window)
                
            }
        }

        
        
    }
    
    func showAnnouncementCampaign(campaign: Campaign, otherCampaigns:[Campaign]) {
        if let announcementView = AnnouncementView.fromNib() as? AnnouncementView where currentShownView == nil {
            
            if let window = UIApplication.sharedApplication().keyWindow {
                currentShownView = announcementView
                announcementView.otherCampaigns = otherCampaigns
                announcementView.campaign = campaign
                announcementView.delegate = self
                announcementView.showOnWindow(window)
                                
            }
        }
    }
    
    func showExpandedAnnouncement(campaign: Campaign) {
    
        if let announcementView = AnnouncementExpandedView.fromNib() as? AnnouncementExpandedView, window = UIApplication.sharedApplication().keyWindow {
            
            currentShownView = announcementView
            announcementView.campaign = campaign
            announcementView.delegate = self
            announcementView.showOnWindow(window)
            
        }
    }
    
    
    func showNPSCampaign(campaign: Campaign, otherCampaigns: [Campaign]) {
     
     
        if let npsContainer = NPSContainerView.fromNib() as? NPSContainerView, npsView = NPSView.fromNib() as? NPSView where currentShownView == nil {
            
            if let window = UIApplication.sharedApplication().keyWindow {
                currentShownView = npsContainer
                npsContainer.delegate = self
                npsContainer.campaign = campaign
                npsView.campaign = campaign
                npsView.otherCampaigns = otherCampaigns
                npsContainer.showOnWindow(window)
                npsContainer.popUpContainer(initialView: npsView)
            }
        }else{
            LoggerManager.log("Error Loading Nib")
        }
    }
    
    func showConversationList(){

        let conversationListController = ConversationListViewController.navigationController()
        TopController.viewController()?.presentViewController(conversationListController, animated: true, completion: nil)
        
    }
    
    func showConversationVC(conversationId: Int) {
        if let topVC = TopController.viewController()  {
            let navVC = ConversationViewController.navigationController(ConversationViewController.ConversationType.ContinueConversation(conversationId: conversationId))
            topVC.presentViewController(navVC, animated: true, completion: nil)
        }
    }
    
    func showNewConversationVC(authorId: Int?) {
        if let topVC = TopController.viewController()  {
            let navVC = ConversationViewController.navigationController(ConversationViewController.ConversationType.CreateConversation(authorId: authorId))
            topVC.presentViewController(navVC, animated: true, completion: nil)
        }
    }
    
    ///Presentation Delegate
    
    func campaignDidFinishWithResponse(view: CampaignView, campaign: Campaign, response: CampaignResponse) {<commit_msg>add check if conversation is presenting, don't show an alert while the list or a conversation is shown<commit_after>//
//  PresentationManager.swift
//  Drift
//
//  Created by <NAME> on 26/01/2016.
//  Copyright © 2016 Drift. All rights reserved.
//

import UIKit

protocol PresentationManagerDelegate:class {
    
    func campaignDidFinishWithResponse(view: CampaignView, campaign: Campaign, response: CampaignResponse)
    func messageViewDidFinish(view: CampaignView)
}


///Responsible for showing a campaign
class PresentationManager: PresentationManagerDelegate {
    
    static var sharedInstance: PresentationManager = PresentationManager()
    weak var currentShownView: CampaignView?
    
    init () {}
    
    func didRecieveCampaigns(campaigns: [Campaign]) {
        
        ///Show latest first
        let sortedCampaigns = campaigns.sort {
            
            if let d1 = $0.createdAt, d2 = $1.createdAt {
                return d1.compare(d2) == .OrderedAscending
            }else{
                return false
            }
        }
        
        var nextCampaigns = [Campaign]()
        
        if campaigns.count > 1 {
            nextCampaigns = Array(sortedCampaigns.dropFirst())
        }
        

        dispatch_async(dispatch_get_main_queue()) { () -> Void in
         
            if let firstCampaign = sortedCampaigns.first, type = firstCampaign.messageType  {
                
                switch type {
                    
                case .Announcement:
                    self.showAnnouncementCampaign(firstCampaign, otherCampaigns: nextCampaigns)
                case .NPS:
                    self.showNPSCampaign(firstCampaign, otherCampaigns: nextCampaigns)
                case .NPSResponse:
                    ()
                }
            }
        }
    }
    
    func didRecieveNewMessages(messages: [(conversationId: Int, messages: [Message])]) {
        
        if let newMessageView = NewMessageView.fromNib() as? NewMessageView where currentShownView == nil && !conversationIsPresenting() {
            
            if let window = UIApplication.sharedApplication().keyWindow {
                currentShownView = newMessageView
                
                let currentConversation = messages.first!
                let otherConversations = messages.filter({ $0.conversationId != currentConversation.conversationId })
                newMessageView.otherConversations = otherConversations                
                newMessageView.conversation = currentConversation
                newMessageView.delegate = self
                newMessageView.showOnWindow(window)
                
            }
        }

        
        
    }
    
    func showAnnouncementCampaign(campaign: Campaign, otherCampaigns:[Campaign]) {
        if let announcementView = AnnouncementView.fromNib() as? AnnouncementView where currentShownView == nil && !conversationIsPresenting() {
            
            if let window = UIApplication.sharedApplication().keyWindow {
                currentShownView = announcementView
                announcementView.otherCampaigns = otherCampaigns
                announcementView.campaign = campaign
                announcementView.delegate = self
                announcementView.showOnWindow(window)
                                
            }
        }
    }
    
    func showExpandedAnnouncement(campaign: Campaign) {
    
        if let announcementView = AnnouncementExpandedView.fromNib() as? AnnouncementExpandedView, window = UIApplication.sharedApplication().keyWindow where !conversationIsPresenting() {
            
            currentShownView = announcementView
            announcementView.campaign = campaign
            announcementView.delegate = self
            announcementView.showOnWindow(window)
            
        }
    }
    
    
    func showNPSCampaign(campaign: Campaign, otherCampaigns: [Campaign]) {
     
     
        if let npsContainer = NPSContainerView.fromNib() as? NPSContainerView, npsView = NPSView.fromNib() as? NPSView where currentShownView == nil && !conversationIsPresenting(){
            
            if let window = UIApplication.sharedApplication().keyWindow {
                currentShownView = npsContainer
                npsContainer.delegate = self
                npsContainer.campaign = campaign
                npsView.campaign = campaign
                npsView.otherCampaigns = otherCampaigns
                npsContainer.showOnWindow(window)
                npsContainer.popUpContainer(initialView: npsView)
            }
        }else{
            LoggerManager.log("Error Loading Nib")
        }
    }
    
    func conversationIsPresenting() -> Bool{
        if let topVC = TopController.viewController() where topVC.classForCoder == ConversationListViewController.classForCoder() || topVC.classForCoder == ConversationViewController.classForCoder(){
            return true
        }
        return false
    }
    
    func showConversationList(){

        let conversationListController = ConversationListViewController.navigationController()
        TopController.viewController()?.presentViewController(conversationListController, animated: true, completion: nil)
        
    }
    
    func showConversationVC(conversationId: Int) {
        if let topVC = TopController.viewController()  {
            let navVC = ConversationViewController.navigationController(ConversationViewController.ConversationType.ContinueConversation(conversationId: conversationId))
            topVC.presentViewController(navVC, animated: true, completion: nil)
        }
    }
    
    func showNewConversationVC(authorId: Int?) {
        if let topVC = TopController.viewController()  {
            let navVC = ConversationViewController.navigationController(ConversationViewController.ConversationType.CreateConversation(authorId: authorId))
            topVC.presentViewController(navVC, animated: true, completion: nil)
        }
    }
    
    ///Presentation Delegate
    
    func campaignDidFinishWithResponse(view: CampaignView, campaign: Campaign, response: CampaignResponse) {
{'fraction_non_alphanumeric': 0.07029459207063418, 'fraction_numerical': 0.003395874013074115, 'mean_word_length': 2.0934873949579833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1872541', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2855, 'n_words': 576}