<commit_before>//
//  SffModelParser.swift
//  Conventions
//
//  Created by <NAME> on 2/6/16.
//  Copyright © 2016 Amai. All rights reserved.
//

import Foundation
import UIKit

class SffEventsParser {
    func parse(data data:NSData!) -> Array<ConventionEvent>! {
        return parse(data: data, halls: Convention.instance.halls);
    }
    
    func parse(data data: NSData!, halls: Array<Hall>!) -> Array<ConventionEvent>! {
        var result = Array<ConventionEvent>();
        
        guard let deserializedEvents =
            try? NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSArray,
            events = deserializedEvents
        else {
            print("Failed to deserialize cached events");
            return result;
        }
        
        for event in events {
            guard let eventId = event["id"] as? String else {
                print("Got event without ID. Skipping");
                continue;
            }
            guard let eventType = event["track"] as? String else {
                print("Got event without type. Skipping. ID=", eventId);
                continue;
            }
            guard let title = event["title"] as? String else {
                print("Event missing title. Skipping. ID=", eventId);
                continue;
            }
            guard let description = event["description"] as? String else {
                print("Event missing description. Skipping. ID=", eventId);
                continue;
            }
            guard let startTime = event["time"]??["start"] as? String else {
                print("Event missing startTime. Skipping. ID=", eventId);
                continue;
            }
            guard let endTime = event["time"]??["end"] as? String else {
                print("Event missing endTime. Skipping. ID=", eventId);
                continue;
            }
            guard let hallName = event["location"] as? String else {
                print("Event missing room. Skipping. ID=", eventId);
                continue;
            }
            
            var speaker = ""
            if let speakers = event["speakers"] as? NSArray {
                speaker = speakers.count > 0 ? speakers.componentsJoinedByString(",") : ""
            }
            
            let conventionEvent = ConventionEvent(
                id: eventId,
                serverId: Int(eventId) ?? 0,
                color: Colors.eventTimeDefaultBackgroundColor,
                textColor: nil,
                title: String(htmlEncodedString: title),
                lecturer: String(htmlEncodedString: speaker),
                startTime: parseDate(startTime),
                endTime: parseDate(endTime),
                type: EventType(
                    backgroundColor: nil,
                    description: eventType),
                hall: findHallByName(halls, hallName: hallName),
                description: parseEventDescription(description));
            
            result.append(conventionEvent);
            
        }
        
        return result;
    }
    
    private func findHallByName(halls: Array<Hall>, hallName: String) -> Hall {
        if let hall = halls.filter({hall in hall.name == hallName}).first {
            return hall;
        }
        
        return Hall(name: "", order: 100);
    }
    
    private func parseDate(time: String) -> NSDate {
        if let result = NSDate.parse(time, dateFormat: "yyyy-MM-dd'T'HH:mm:ssxxxxx") {
            return result;
        }
        
        return NSDate()
    }
    
    private func parseEventDescription(eventDescription : String?) -> String? {
        return eventDescription?
            .replace(pattern: "<img", withTemplate: "<ximg")?
            .replace(pattern: "/img>", withTemplate: "/ximg>")?
            .replace(pattern: "<iframe", withTemplate: "<xiframe")?
            .replace(pattern: "iframe>", withTemplate: "xiframe>");
    }
}

extension String {
    init(htmlEncodedString: String) {
        do {
            let encodedData = htmlEncodedString.dataUsingEncoding(NSUTF8StringEncoding)!
            let attributedOptions : [String: AnyObject] = [
                NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
            ]<commit_msg>Fixed events without halls showing in the programme<commit_after>//
//  SffModelParser.swift
//  Conventions
//
//  Created by <NAME> on 2/6/16.
//  Copyright © 2016 Amai. All rights reserved.
//

import Foundation
import UIKit

class SffEventsParser {
    func parse(data data:NSData!) -> Array<ConventionEvent>! {
        return parse(data: data, halls: Convention.instance.halls);
    }
    
    func parse(data data: NSData!, halls: Array<Hall>!) -> Array<ConventionEvent>! {
        var result = Array<ConventionEvent>();
        
        guard let deserializedEvents =
            try? NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSArray,
            events = deserializedEvents
        else {
            print("Failed to deserialize cached events");
            return result;
        }
        
        for event in events {
            guard let eventId = event["id"] as? String else {
                print("Got event without ID. Skipping");
                continue;
            }
            guard let eventType = event["track"] as? String else {
                print("Got event without type. Skipping. ID=", eventId);
                continue;
            }
            guard let title = event["title"] as? String else {
                print("Event missing title. Skipping. ID=", eventId);
                continue;
            }
            guard let description = event["description"] as? String else {
                print("Event missing description. Skipping. ID=", eventId);
                continue;
            }
            guard let startTime = event["time"]??["start"] as? String else {
                print("Event missing startTime. Skipping. ID=", eventId);
                continue;
            }
            guard let endTime = event["time"]??["end"] as? String else {
                print("Event missing endTime. Skipping. ID=", eventId);
                continue;
            }
            guard
                let hallName = event["location"] as? String,
                let hall = findHallByName(halls, hallName: hallName)
            else {
                print("Event missing location. Skipping. ID=", eventId, " name=", title);
                continue;
            }
            
            var speaker = ""
            if let speakers = event["speakers"] as? NSArray {
                speaker = speakers.count > 0 ? speakers.componentsJoinedByString(",") : ""
            }
            
            let conventionEvent = ConventionEvent(
                id: eventId,
                serverId: Int(eventId) ?? 0,
                color: Colors.eventTimeDefaultBackgroundColor,
                textColor: nil,
                title: String(htmlEncodedString: title),
                lecturer: String(htmlEncodedString: speaker),
                startTime: parseDate(startTime),
                endTime: parseDate(endTime),
                type: EventType(
                    backgroundColor: nil,
                    description: eventType),
                hall: hall,
                description: parseEventDescription(description));
            
            result.append(conventionEvent);
            
        }
        
        return result;
    }
    
    private func findHallByName(halls: Array<Hall>, hallName: String) -> Hall? {
        if let hall = halls.filter({hall in hall.name == hallName}).first {
            return hall
        }
        
        return nil
    }
    
    private func parseDate(time: String) -> NSDate {
        if let result = NSDate.parse(time, dateFormat: "yyyy-MM-dd'T'HH:mm:ssxxxxx") {
            return result;
        }
        
        return NSDate()
    }
    
    private func parseEventDescription(eventDescription : String?) -> String? {
        return eventDescription?
            .replace(pattern: "<img", withTemplate: "<ximg")?
            .replace(pattern: "/img>", withTemplate: "/ximg>")?
            .replace(pattern: "<iframe", withTemplate: "<xiframe")?
            .replace(pattern: "iframe>", withTemplate: "xiframe>");
    }
}

extension String {
    init(htmlEncodedString: String) {
        do {
            let encodedData = htmlEncodedString.dataUsingEncoding(NSUTF8StringEncoding)!
            let attributedOptions : [String: AnyObject] = [
                NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
            ]
{'fraction_non_alphanumeric': 0.10223387280601778, 'fraction_numerical': 0.0030772737633918395, 'mean_word_length': 1.7133580705009277, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '5319602', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2162, 'n_words': 576}