<commit_before>//
//  DecksViewController.swift
//  StudyBox_iOS
//
//  Created by <NAME> on 03.03.2016.
//  Copyright © 2016 BLStream. All rights reserved.
//

import UIKit


class DecksViewController: StudyBoxViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    private var decksArray: [Deck]?
    
    lazy private var dataManager:DataManager? = {
        return UIApplication.appDelegate().dataManager
    }()
    
    @IBOutlet var decksCollectionView: UICollectionView!
    
    // TODO: in future replace managerWithDummyData()
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        decksArray = dataManager?.decks(true)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        decksCollectionView.delegate = self
        decksCollectionView.dataSource = self
        
        let layout = decksCollectionView.collectionViewLayout
        let flow = layout as! UICollectionViewFlowLayout
        let spacing = Utils.DeckViewLayout.DecksSpacing
        equalSizeAndSpacing(numberOfCellsInRow: Utils.DeckViewLayout.DecksInRowIPhoneVer, spacing: spacing, collectionFlowLayout: flow)
        
        decksCollectionView.backgroundColor = UIColor.whiteColor()
        
    }
    
    // this function calculate size of decks, by given spacing and number of cells in row
    private func equalSizeAndSpacing(numberOfCellsInRow crNumber: CGFloat, spacing: CGFloat,
        collectionFlowLayout flow: UICollectionViewFlowLayout){
            
            let screenSize = self.view.bounds.size
            let deckWidth = screenSize.width/crNumber - (spacing + spacing/crNumber)
            flow.sectionInset = UIEdgeInsetsMake(spacing, spacing, spacing, spacing)
            // spacing between decks
            flow.minimumInteritemSpacing = spacing
            // spacing between rows
            flow.minimumLineSpacing = spacing
            // size for every deck
            flow.itemSize = CGSize(width: deckWidth, height: deckWidth)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    // Calculate number of decks. If no decks, return 0
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        var cellsNumber = 0
        if let decksArrayCount = decksArray?.count{
            cellsNumber = decksArrayCount
        }
        return cellsNumber
    }
    
    // Populate cells with decks data. Change cells style
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Utils.UIIds.DecksViewCellID, forIndexPath: indexPath)
            as! DecksViewCell
        
        cell.layoutIfNeeded()
        
        if var deckName = decksArray?[indexPath.row].name{
            if deckName.isEmpty {
                deckName = Utils.DeckViewLayout.DeckWithoutTitle
            }
            cell.deckNameLabel.text = deckName
        }
        // changing label UI
        cell.deckNameLabel.adjustFontSizeToHeight(UIFont.sbFont(size: sbFontSizeLarge, bold: false), max: sbFontSizeLarge, min: sbFontSizeSmall)
        cell.deckNameLabel.textColor = UIColor.whiteColor()
        cell.deckNameLabel.numberOfLines = 0
        // adding line breaks
        cell.deckNameLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        cell.deckNameLabel.preferredMaxLayoutWidth = cell.bounds.size.width
        cell.contentView.backgroundColor = UIColor.sb_Graphite()
        
        return cell
    }
    
    
    // When cell tapped, change to test
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if let deck = decksArray?[indexPath.row] {
            
            do {
                
                if let flashcards = try dataManager?.flashcards(forDeckWithId: deck.id) {
                   
                    let alert = UIAlertController(title: "Test or Learn?", message: "Choose the mode which you would like to start", preferredStyle: .Alert)
                    
                    let testButton = UIAlertAction(title: "Test", style: .Default){ (alert: UIAlertAction!) -> Void in
                        self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Test(5)))
                    }
                    
                    let studyButton = UIAlertAction(title: "Learn", style: .Default) { (alert: UIAlertAction!) -> Void in
                        self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Learn))
                    }
                    
                    alert.addAction(testButton)
                    alert.addAction(studyButton)

                    presentViewController(alert, animated: true, completion:nil)
                    
                }
            }catch let e {
                debugPrint(e)
            }
        }
        
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "StartTest", let testViewController = segue.destinationViewController as? TestViewController, let testLogic = sender as? Test {
            testViewController.testLogicSource = testLogic
        }
    }
    
}

// this extension dynamically change the size of the fonts, so text can fit<commit_msg>Add UIAlertController with option to choose amount of flashcards in the test<commit_after>//
//  DecksViewController.swift
//  StudyBox_iOS
//
//  Created by <NAME> on 03.03.2016.
//  Copyright © 2016 BLStream. All rights reserved.
//

import UIKit


class DecksViewController: StudyBoxViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    private var decksArray: [Deck]?
    
    lazy private var dataManager:DataManager? = {
        return UIApplication.appDelegate().dataManager
    }()
    
    @IBOutlet var decksCollectionView: UICollectionView!
    
    // TODO: in future replace managerWithDummyData()
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        decksArray = dataManager?.decks(true)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        decksCollectionView.delegate = self
        decksCollectionView.dataSource = self
        
        let layout = decksCollectionView.collectionViewLayout
        let flow = layout as! UICollectionViewFlowLayout
        let spacing = Utils.DeckViewLayout.DecksSpacing
        equalSizeAndSpacing(numberOfCellsInRow: Utils.DeckViewLayout.DecksInRowIPhoneVer, spacing: spacing, collectionFlowLayout: flow)
        
        decksCollectionView.backgroundColor = UIColor.whiteColor()
        
    }
    
    // this function calculate size of decks, by given spacing and number of cells in row
    private func equalSizeAndSpacing(numberOfCellsInRow crNumber: CGFloat, spacing: CGFloat,
        collectionFlowLayout flow: UICollectionViewFlowLayout){
            
            let screenSize = self.view.bounds.size
            let deckWidth = screenSize.width/crNumber - (spacing + spacing/crNumber)
            flow.sectionInset = UIEdgeInsetsMake(spacing, spacing, spacing, spacing)
            // spacing between decks
            flow.minimumInteritemSpacing = spacing
            // spacing between rows
            flow.minimumLineSpacing = spacing
            // size for every deck
            flow.itemSize = CGSize(width: deckWidth, height: deckWidth)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    // Calculate number of decks. If no decks, return 0
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        var cellsNumber = 0
        if let decksArrayCount = decksArray?.count{
            cellsNumber = decksArrayCount
        }
        return cellsNumber
    }
    
    // Populate cells with decks data. Change cells style
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Utils.UIIds.DecksViewCellID, forIndexPath: indexPath)
            as! DecksViewCell
        
        cell.layoutIfNeeded()
        
        if var deckName = decksArray?[indexPath.row].name{
            if deckName.isEmpty {
                deckName = Utils.DeckViewLayout.DeckWithoutTitle
            }
            cell.deckNameLabel.text = deckName
        }
        // changing label UI
        cell.deckNameLabel.adjustFontSizeToHeight(UIFont.sbFont(size: sbFontSizeLarge, bold: false), max: sbFontSizeLarge, min: sbFontSizeSmall)
        cell.deckNameLabel.textColor = UIColor.whiteColor()
        cell.deckNameLabel.numberOfLines = 0
        // adding line breaks
        cell.deckNameLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        cell.deckNameLabel.preferredMaxLayoutWidth = cell.bounds.size.width
        cell.contentView.backgroundColor = UIColor.sb_Graphite()
        
        return cell
    }
    
    
    // When cell tapped, change to test
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if let deck = decksArray?[indexPath.row] {
            
            do {
                
                if let flashcards = try dataManager?.flashcards(forDeckWithId: deck.id) {
                   
                    let alert = UIAlertController(title: "Test or Learn?", message: "Choose the mode which you would like to start", preferredStyle: .Alert)
                    
                    let testButton = UIAlertAction(title: "Test", style: .Default){ (alert: UIAlertAction!) -> Void in
                        let alertAmount = UIAlertController(title: "How many flashcards?", message: "Choose amount of flashcards in the test", preferredStyle: .Alert)
                        
                        let amountOne = UIAlertAction(title: "1", style: .Default) { (alert: UIAlertAction!) -> Void in
                            self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Test(1)))
                        }
                        
                        let amountFive = UIAlertAction(title: "5", style: .Default) { (alert: UIAlertAction!) -> Void in
                            self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Test(5)))
                        }
                        
                        let amountTen = UIAlertAction(title: "10", style: .Default) { (alert: UIAlertAction!) -> Void in
                            self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Test(10)))
                        }
                        
                        let amountFifteen = UIAlertAction(title: "15", style: .Default) { (alert: UIAlertAction!) -> Void in
                            self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Test(15)))
                        }
                        
                        let amountTwenty = UIAlertAction(title: "20", style: .Default) { (alert: UIAlertAction!) -> Void in
                            self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Test(20)))
                        }
                        
                        alertAmount.addAction(amountOne)
                        alertAmount.addAction(amountFive)
                        alertAmount.addAction(amountTen)
                        alertAmount.addAction(amountFifteen)
                        alertAmount.addAction(amountTwenty)
                        
                        self.presentViewController(alertAmount, animated: true, completion:nil)

                    }
                    
                    let studyButton = UIAlertAction(title: "Learn", style: .Default) { (alert: UIAlertAction!) -> Void in
                        self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Learn))
                    }
                    
                    alert.addAction(testButton)
                    alert.addAction(studyButton)

                    presentViewController(alert, animated: true, completion:nil)
                    
                }
            }catch let e {
                debugPrint(e)
            }
        }
        
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "StartTest", let testViewController = segue.destinationViewController as? TestViewController, let testLogic = sender as? Test {
            testViewController.testLogicSource = testLogic
        }
    }
    
}

// this extension dynamically change the size of the fonts, so text can fit
{'fraction_non_alphanumeric': 0.0760665111729742, 'fraction_numerical': 0.003686705289293507, 'mean_word_length': 2.0563347896068063, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, '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': '4108769', 'n_tokens_mistral': 3523, 'n_tokens_neox': 3338, 'n_words': 803}