<commit_before>//
//  PlantInfoViewController.swift
//  TTFFCamp
//
//  Created by yanze on 3/9/16.
//  Copyright © 2016 The Taylor Family Foundation. All rights reserved.
//

import UIKit
import AVFoundation
import Auk


class PlantInfoViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
    
    @IBOutlet weak var plantNameButton: UIButton!
    @IBOutlet weak var locationButton: UIButton!
    @IBOutlet weak var originButton: UIButton!
    @IBOutlet weak var descriptionButton: UIButton!
    @IBOutlet weak var whenToPlantButton: UIButton!
    @IBOutlet weak var coolFactButton: UIButton!
//    @IBOutlet weak var plantImage: UIImageView!
    
    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var originTextLabel: UILabel!
    @IBOutlet weak var locationTextLabel: UILabel!
    @IBOutlet weak var descriptionTextView: UITextView!
    @IBOutlet weak var whenToPlantTextLabel: UILabel!
    @IBOutlet weak var coolFactsTextView: UITextView!
    
    
    //MARK: NSSpeech
    let synth = AVSpeechSynthesizer()
    var myUtterance = AVSpeechUtterance(string: "")
    
    var singlePlant = Plant()
    var allPlants: [Plant] = []
    
    var detectedText = ""
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        
        self.navigationItem.setHidesBackButton(true, animated: false)
        
        customButtons()
        
        // get all plants from local storage
        allPlants = Database.all()
        getPlantByName(detectedText)
        initializeScrollView()
        
    }
    
    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)    
        self.navigationItem.setHidesBackButton(editing, animated: animated)
    }
    
    
    func initializeScrollView(){
        for i in 0 ..< singlePlant.images.count {
            let label = UILabel(frame: CGRect(x: 10 + (i*500), y: 10, width: 50, height: 100))
            label.text = singlePlant.captions[i]
            
            
            // Decode Base64 string into NSData
            let imageData = NSData(base64EncodedString: singlePlant.images[i], options: NSDataBase64DecodingOptions(rawValue: 0))
            
            // Set image variable to UIImage from raw data
            let imageView = UIImageView(frame: CGRect(x: (i*500), y: 0, width: 500, height: 500))
            imageView.image = UIImage(data: imageData!)
            
            
            scrollView.addSubview(imageView)
            scrollView.addSubview(label)
            
    
        }
        let scrollViewWidth = CGFloat(singlePlant.images.count * 500)
        scrollView.contentSize = CGSizeMake(scrollViewWidth, scrollView.frame.size.height);
    }
    
    
    func getPlantByName(plantName: String){
        // loop through all plants in the local database to find the specific plant scanned or chosen from the list
        for i in 0 ..< allPlants.count {
            if allPlants[i].plantName == plantName {
                print("found the correct plant ID", allPlants[i])
                singlePlant = allPlants[i]
                print(singlePlant.plantName)
                
            }
        }
        
        // Set UILabel properties of a single plant from database
        plantNameButton.setTitle(singlePlant.plantName, forState: UIControlState.Normal)
        originTextLabel.text = singlePlant.origin
        originTextLabel.numberOfLines = 0
        locationTextLabel.text = singlePlant.location
        locationTextLabel.numberOfLines = 0
        descriptionTextView.text = singlePlant.plantDescription
        whenToPlantTextLabel.text = singlePlant.whenToPlant
        whenToPlantTextLabel.numberOfLines = 0
        coolFactsTextView.text = singlePlant.coolFact
        descriptionTextView.editable = false;
        coolFactsTextView.editable = false;

        
        
        // Received image data comes in as Base64 string
        let receivedData = singlePlant.images[0]
        
        // Decode Base64 string into NSData
        let imageData = NSData(base64EncodedString: receivedData, options: NSDataBase64DecodingOptions(rawValue: 0))
        
        // Set image variable to UIImage from raw data
        let image = UIImage(data: imageData!)
        
        // Set layout parameters for image
//        plantImage.image = image
//        plantImage.layer.borderWidth = 3
//        plantImage.layer.borderColor = UIColor.greenColor().CGColor
    }
    
    // Function for text-to-speech
    @IBAction func textToSpeech(sender: UIButton) {
        if !synth.speaking{
            if sender.tag == 0 {
                myUtterance = AVSpeechUtterance(string: plantNameButton.titleLabel!.text!)
            }
            if sender.tag == 1 {
                myUtterance = AVSpeechUtterance(string: "Origin: \(singlePlant.origin)")
            }
            if sender.tag == 2 {
                myUtterance = AVSpeechUtterance(string: "Location: \(singlePlant.location)")
            }
            if sender.tag == 3 {
                myUtterance = AVSpeechUtterance(string: "Description: \(singlePlant.plantDescription)")<commit_msg>clean up the code<commit_after>//
//  PlantInfoViewController.swift
//  TTFFCamp
//
//  Created by yanze on 3/9/16.
//  Copyright © 2016 The Taylor Family Foundation. All rights reserved.
//

import UIKit
import AVFoundation
import Auk


class PlantInfoViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
    
    @IBOutlet weak var plantNameButton: UIButton!
    @IBOutlet weak var locationButton: UIButton!
    @IBOutlet weak var originButton: UIButton!
    @IBOutlet weak var descriptionButton: UIButton!
    @IBOutlet weak var whenToPlantButton: UIButton!
    @IBOutlet weak var coolFactButton: UIButton!
//    @IBOutlet weak var plantImage: UIImageView!
    
    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var originTextLabel: UILabel!
    @IBOutlet weak var locationTextLabel: UILabel!
    @IBOutlet weak var descriptionTextView: UITextView!
    @IBOutlet weak var whenToPlantTextLabel: UILabel!
    @IBOutlet weak var coolFactsTextView: UITextView!
    
    
    //MARK: NSSpeech
    let synth = AVSpeechSynthesizer()
    var myUtterance = AVSpeechUtterance(string: "")
    
    var singlePlant = Plant()
    var allPlants: [Plant] = []
    
    var detectedText = ""
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        
        self.navigationItem.setHidesBackButton(true, animated: false)
        
        customButtons()
        
        // get all plants from local storage
        allPlants = Database.all()
        getPlantByName(detectedText)
        initializeScrollView()
        
    }
    
    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)    
        self.navigationItem.setHidesBackButton(editing, animated: animated)
    }
    
    
    func initializeScrollView(){
        for i in 0 ..< singlePlant.images.count {
            let label = UILabel(frame: CGRect(x: 10 + (i*500), y: 10, width: 50, height: 100))
            label.text = singlePlant.captions[i]
            
            // Decode Base64 string into NSData
            let imageData = NSData(base64EncodedString: singlePlant.images[i], options: NSDataBase64DecodingOptions(rawValue: 0))
            
            // Set image variable to UIImage from raw data
            let imageView = UIImageView(frame: CGRect(x: (i*500), y: 0, width: 500, height: 500))
            imageView.image = UIImage(data: imageData!)
            
            scrollView.addSubview(imageView)
            scrollView.addSubview(label)
    
        }
        let scrollViewWidth = CGFloat(singlePlant.images.count * 500)
        scrollView.contentSize = CGSizeMake(scrollViewWidth, scrollView.frame.size.height);
    }
    
    
    func getPlantByName(plantName: String){
        // loop through all plants in the local database to find the specific plant scanned or chosen from the list
        for i in 0 ..< allPlants.count {
            if allPlants[i].plantName == plantName {
                print("found the correct plant ID", allPlants[i])
                singlePlant = allPlants[i]
                print(singlePlant.plantName)
                
            }
        }
        
        // Set UILabel properties of a single plant from database
        plantNameButton.setTitle(singlePlant.plantName, forState: UIControlState.Normal)
        originTextLabel.text = singlePlant.origin
        originTextLabel.numberOfLines = 0
        locationTextLabel.text = singlePlant.location
        locationTextLabel.numberOfLines = 0
        descriptionTextView.text = singlePlant.plantDescription
        whenToPlantTextLabel.text = singlePlant.whenToPlant
        whenToPlantTextLabel.numberOfLines = 0
        coolFactsTextView.text = singlePlant.coolFact
        descriptionTextView.editable = false;
        coolFactsTextView.editable = false;
    }
    
    // Function for text-to-speech
    @IBAction func textToSpeech(sender: UIButton) {
        if !synth.speaking{
            if sender.tag == 0 {
                myUtterance = AVSpeechUtterance(string: plantNameButton.titleLabel!.text!)
            }
            if sender.tag == 1 {
                myUtterance = AVSpeechUtterance(string: "Origin: \(singlePlant.origin)")
            }
            if sender.tag == 2 {
                myUtterance = AVSpeechUtterance(string: "Location: \(singlePlant.location)")
            }
            if sender.tag == 3 {
                myUtterance = AVSpeechUtterance(string: "Description: \(singlePlant.plantDescription)")
{'fraction_non_alphanumeric': 0.08092251669026906, 'fraction_numerical': 0.01102569289904916, 'mean_word_length': 2.321128653006382, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '351782', 'n_tokens_mistral': 2925, 'n_tokens_neox': 2782, 'n_words': 670}