<commit_before>//
//  SearchResultsViewController.swift
//  PhotoViewer
//
//  Created by <NAME> on 2/27/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

class SearchResultsViewController: UIViewController
{
    @IBOutlet weak var backButton: UIButton!
    @IBOutlet weak var searchTermLabel: UILabel!
    @IBOutlet weak var postsCountLabel: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!
    
    var searchTerm: String!
    private var topPosts = [Post]()
    private var mostRecent = [Post]()
    
    deinit
    {
        print("deinit SearchResultsViewController")
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        searchTermLabel.text = ""
        postsCountLabel.text = ""
        collectionView.dataSource = self
        collectionView.delegate = self
        callInstagramTagService()
    }
    
    @IBAction func buttonTapped(sender: UIButton)
    {
        if sender ==  backButton
        {
            navigationController?.popViewControllerAnimated(true)
        }
    }
    
    private func callInstagramTagService()
    {
        let service = InstagramSearchPostService(tag: searchTerm)
        Loader.show()
        SessionManager.sharedInstance.start(service, suceedHandler: { [unowned self] (response) in
            Loader.dismiss()
            if let response = response
            {
                let result = PostUtility.parseInstagramPostResponse(response)
                self.searchTermLabel.text = "#\(result.name)"
                self.updatePostsCountLabel(result.count)
                self.topPosts.removeAll()
                self.topPosts.appendContentsOf(result.topPosts)
                self.mostRecent.removeAll()
                self.mostRecent.appendContentsOf(result.mostRecent)
                self.collectionView.reloadData()
            }
        }) { (error) in
            Loader.dismiss()
            print("error = \(error)")
            // TODO handler the errors
        }
    }
    
    private func updatePostsCountLabel(count: Int)
    {
        let numberFormatter = NSNumberFormatter()
        numberFormatter.groupingSeparator = ","
        numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle

        let boldAttributed: NSMutableAttributedString!
        if let strCount = numberFormatter.stringFromNumber(count)
        {
            boldAttributed = NSMutableAttributedString(string: strCount, attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(postsCountLabel.font.pointSize)])
        }
        else
        {
            boldAttributed = NSMutableAttributedString(string: "\(count)", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(postsCountLabel.font.pointSize)])
        }
        
        let normalAttributed: NSAttributedString!
        if count == 1
        {
            normalAttributed = NSAttributedString(string: " post")
        }<commit_msg>handling error on search<commit_after>//
//  SearchResultsViewController.swift
//  PhotoViewer
//
//  Created by <NAME> on 2/27/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

class SearchResultsViewController: UIViewController
{
    @IBOutlet weak var backButton: UIButton!
    @IBOutlet weak var searchTermLabel: UILabel!
    @IBOutlet weak var postsCountLabel: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!
    
    var searchTerm: String!
    private var topPosts = [Post]()
    private var mostRecent = [Post]()
    
    deinit
    {
        print("deinit SearchResultsViewController")
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        searchTermLabel.text = ""
        postsCountLabel.text = ""
        collectionView.dataSource = self
        collectionView.delegate = self
        callInstagramTagService()
    }
    
    @IBAction func buttonTapped(sender: UIButton)
    {
        if sender ==  backButton
        {
            back()
        }
    }
    
    private func back()
    {
        navigationController?.popViewControllerAnimated(true)
    }
    
    private func callInstagramTagService()
    {
        let service = InstagramSearchPostService(tag: searchTerm)
        Loader.show()
        SessionManager.sharedInstance.start(service, suceedHandler: { [unowned self] (response) in
            Loader.dismiss()
            if let response = response
            {
                let result = PostUtility.parseInstagramPostResponse(response)
                self.searchTermLabel.text = "#\(result.name)"
                self.updatePostsCountLabel(result.count)
                self.topPosts.removeAll()
                self.topPosts.appendContentsOf(result.topPosts)
                self.mostRecent.removeAll()
                self.mostRecent.appendContentsOf(result.mostRecent)
                self.collectionView.reloadData()
            }
        }) { [weak self] (error) in
            Loader.dismiss()
            print("error = \(error)")
            Components.displayAlertWithTitle("Error", message: "Error searching for photos, please try again.", buttonTitle: "Accept", buttonHandler: {
                self?.back()
            })
        }
    }
    
    private func updatePostsCountLabel(count: Int)
    {
        let numberFormatter = NSNumberFormatter()
        numberFormatter.groupingSeparator = ","
        numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle

        let boldAttributed: NSMutableAttributedString!
        if let strCount = numberFormatter.stringFromNumber(count)
        {
            boldAttributed = NSMutableAttributedString(string: strCount, attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(postsCountLabel.font.pointSize)])
        }
        else
        {
            boldAttributed = NSMutableAttributedString(string: "\(count)", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(postsCountLabel.font.pointSize)])
        }
        
        let normalAttributed: NSAttributedString!
        if count == 1
        {
            normalAttributed = NSAttributedString(string: " post")
        }
{'fraction_non_alphanumeric': 0.0868782722513089, 'fraction_numerical': 0.0032722513089005235, 'mean_word_length': 2.2865591397849463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3128533', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1581, 'n_words': 320}