//
//  ViewController.swift
//  Covid_Requirements
//
//  Created by Paritosh Arora on 24/04/21.
//

import UIKit
import CoreML
import SwifteriOS
class ViewController: UIViewController {
    var resultArr=[String]()
    var uriArr=[String]()
    var buttonTapped=false
    var flagR=0
    let classifier = TweetSentimentClassifier()
    
    let swifter = Swifter(consumerKey:Constants.API_KEY,consumerSecret:Constants.API_SECRET)
    @IBOutlet weak var content: UITableView!
    @IBOutlet weak var cityLbl: UITextField!
    @IBOutlet weak var optionBtn: UIButton!
    @IBOutlet weak var bedBtn: UIButton!
    @IBOutlet weak var vaccineBtn: UIButton!
    override func viewDidLoad() {
        self.cityLbl.delegate = self
        content.delegate=self
        content.dataSource=self
        super.viewDidLoad()
        swifter.searchTweet(using: "Covid India",lang:"en",count:20,tweetMode: .extended, success:{(results,metadata)in
            self.resultArr.removeAll()
            self.uriArr.removeAll()
            for i in (0...50){
                let prediction = try! self.classifier.prediction(text: results[i]["full_text"].string ?? "angry")
                if(prediction.label=="Pos"||prediction.label=="Neutral"){
                self.resultArr.append(results[i]["full_text"].string ?? "error loading")
                    self.uriArr.append(results[i]["id_str"].string ?? " ")
                    
                }
            }
            if let tweet=results[0]["full_text"].string{
                self.content.reloadData()
               
//                print(tweet)
            }
//            print(results)
        }){
            (error) in print("There was a error \(error)")
          
        }

        // Do any additional setup after loading the view.
    }

    @IBAction func optionBtnClicked(_ sender: UIButton) {
       
            optionBtn.backgroundColor=UIColor.cyan
            optionBtn.layer.cornerRadius=15
            bedBtn.backgroundColor=UIColor.clear
            vaccineBtn.backgroundColor=UIColor  .clear
                    
       
        
        
        swifter.searchTweet(using: "oxygen cylinder available \(cityLbl.text!)",lang:"en",count:20,tweetMode: .extended, success:{(results,metadata)in
            self.resultArr.removeAll()
            self.uriArr.removeAll()
            for i in (0...50){
                let prediction = try! self.classifier.prediction(text: results[i]["full_text"].string ?? "angry")
                if(prediction.label=="Pos"||prediction.label=="Neutral"){
                self.resultArr.append(results[i]["full_text"].string ?? "error loading")
                    self.uriArr.append(results[i]["id_str"].string ?? " ")
                    
                }
            }
            if let tweet=results[0]["full_text"].string{
                self.content.reloadData()
               
//                print(tweet)
            }
        }){
            (error) in print("There was a error \(error)")
            
        }
    }
    
    @IBAction func vaccineBtn(_ sender: UIButton) {
        vaccineBtn.backgroundColor=UIColor.cyan
        vaccineBtn.layer.cornerRadius=15
        bedBtn.backgroundColor=UIColor.clear
        optionBtn.backgroundColor=UIColor  .clear
        swifter.searchTweet(using: "vaccines available \(cityLbl.text!)",lang:"en",count:20,tweetMode: .extended, success:{(results,metadata)in
            self.resultArr.removeAll()
            self.uriArr.removeAll()
            for i in (0...50){
                let prediction = try! self.classifier.prediction(text: results[i]["full_text"].string ?? "angry")
                if(prediction.label=="Pos"||prediction.label=="Neutral"){
                self.resultArr.append(results[i]["full_text"].string ?? "error loading")
                    self.uriArr.append(results[i]["id_str"].string ?? " ")
                    
                }
            }
            if let tweet=results[0]["full_text"].string{
                self.content.reloadData()
               
//                print(tweet)
            }
//            print(results)
        }){
            (error) in print("There was a error \(error)")
          
        }
    }
    @IBAction func bedsBtn(_ sender: UIButton) {
        bedBtn.backgroundColor=UIColor.cyan
        bedBtn.layer.cornerRadius=15
        optionBtn.backgroundColor=UIColor.clear
        vaccineBtn.backgroundColor=UIColor  .clear
        swifter.searchTweet(using: "icu rooms available \(cityLbl.text!)",lang:"en",count:20,tweetMode: .extended, success:{(results,metadata)in
            self.resultArr.removeAll()
            self.uriArr.removeAll()
            for i in (0...50){
                let prediction = try! self.classifier.prediction(text: results[i]["full_text"].string ?? "angry")
                if(prediction.label=="Pos"||prediction.label=="Neutral"){
                self.resultArr.append(results[i]["full_text"].string ?? "error loading")
                    self.uriArr.append(results[i]["id_str"].string ?? " ")
                    
                }
            }
            if let tweet=results[0]["full_text"].string{
                self.content.reloadData()
               
//                print(tweet)
            }
        }){
            (error) in print("There was a error \(error)")
        }
    }
  
}

//MARK: Interaction of cells
extension ViewController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let baseUrl="https://twitter.com/i/web/status/"
        if let url = URL(string: "\(baseUrl)"+uriArr[indexPath.row]) {
            UIApplication.shared.open(url)
        }
    }
}
extension ViewController:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return resultArr.count;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=content.dequeueReusableCell(withIdentifier: "cell",for:indexPath)
        print(resultArr)
        cell.textLabel?.text=resultArr[indexPath.row]
        return cell
    }
}
//MARK: TEXT FIELD
extension ViewController:UITextFieldDelegate{
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
              return false
    }
}
