<commit_before>//
//  CallScriptViewController.swift
//  FiveCalls
//
//  Created by <NAME> on 2/3/17.
//

import UIKit
import CoreLocation
import DropDown

class CallScriptViewController : UIViewController, IssueShareable {
    
    var issuesManager: IssuesManager!
    var issue: Issue!
    var contact: Contact!
    var logs = ContactLogs.load()
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var footer: UIView!
    @IBOutlet weak var resultUnavailableButton: ContactButton!
    @IBOutlet weak var resultVoicemailButton: ContactButton!
    @IBOutlet weak var resultContactedButton: ContactButton!
    @IBOutlet weak var resultSkipButton: ContactButton!
    @IBOutlet weak var resultNextButton: ContactButton!
    @IBOutlet weak var footerLabel: UILabel!
    @IBOutlet weak var footerHeightContraint: NSLayoutConstraint!
    var dropdown: DropDown?
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.tintColor = .white
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(IssueDetailViewController.shareButtonPressed(_ :)))
        
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
        
        setupView()
    }
    
    func setupView() {
        guard let issue = issue, let issueIndex = issue.contacts.index(where:{$0.id == contact.id}) else { return }
        title = "Contact \(issueIndex+1) of \(issue.contacts.count)"

        let hasCompleted = logs.hasCompleted(issue: issue.id, allContacts: issue.contacts)
        let hasContacted = logs.hasContacted(contactId: contact.id, forIssue: issue.id)
        if hasCompleted {
            self.footerHeightContraint.constant = 30
            self.footerLabel.text = "You've contacted everyone, great work!"
        } else {
            self.footerHeightContraint.constant = hasContacted ? 75 : 118
            self.footerLabel.text = hasContacted ? "You've already contacted \(self.contact.name)." : "Enter your call result to get the next call."
        }
        self.resultNextButton.isHidden = hasCompleted || !hasContacted
        self.resultUnavailableButton.isHidden = hasCompleted || hasContacted
        self.resultVoicemailButton.isHidden = hasCompleted || hasContacted
        self.resultContactedButton.isHidden = hasCompleted || hasContacted
        self.resultSkipButton.isHidden = hasCompleted || hasContacted
        
        footer.setNeedsUpdateConstraints()
    }
    
    func callButtonPressed(_ button: UIButton) {
        callNumber(contact.phone)
    }

    func callNumber(_ number: String) {
        print("dialing \(number)")
        if let dialURL = URL(string: "telprompt:\(number)") {
            UIApplication.shared.open(dialURL) { success in
                //Log the result
            }
        }
    }
    
    func reportCallOutcome(_ log: ContactLog) {
        if log.outcome.characters.count > 0 {
            logs.add(log: log)
            let operation = ReportOutcomeOperation(log:log)
            OperationQueue.main.addOperation(operation)
        }
    }
    
    @IBAction func resultButtonPressed(_ button: UIButton) {
        var outcomeType = ""
        switch button {
        case resultContactedButton:
            outcomeType = "contacted"
            break
        case resultSkipButton:
            outcomeType = "skip"
            break
        case resultVoicemailButton:<commit_msg>use deprecated `openURL` method when necessary.<commit_after>//
//  CallScriptViewController.swift
//  FiveCalls
//
//  Created by <NAME> on 2/3/17.
//

import UIKit
import CoreLocation
import DropDown

class CallScriptViewController : UIViewController, IssueShareable {
    
    var issuesManager: IssuesManager!
    var issue: Issue!
    var contact: Contact!
    var logs = ContactLogs.load()
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var footer: UIView!
    @IBOutlet weak var resultUnavailableButton: ContactButton!
    @IBOutlet weak var resultVoicemailButton: ContactButton!
    @IBOutlet weak var resultContactedButton: ContactButton!
    @IBOutlet weak var resultSkipButton: ContactButton!
    @IBOutlet weak var resultNextButton: ContactButton!
    @IBOutlet weak var footerLabel: UILabel!
    @IBOutlet weak var footerHeightContraint: NSLayoutConstraint!
    var dropdown: DropDown?
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.tintColor = .white
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(IssueDetailViewController.shareButtonPressed(_ :)))
        
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
        
        setupView()
    }
    
    func setupView() {
        guard let issue = issue, let issueIndex = issue.contacts.index(where:{$0.id == contact.id}) else { return }
        title = "Contact \(issueIndex+1) of \(issue.contacts.count)"

        let hasCompleted = logs.hasCompleted(issue: issue.id, allContacts: issue.contacts)
        let hasContacted = logs.hasContacted(contactId: contact.id, forIssue: issue.id)
        if hasCompleted {
            self.footerHeightContraint.constant = 30
            self.footerLabel.text = "You've contacted everyone, great work!"
        } else {
            self.footerHeightContraint.constant = hasContacted ? 75 : 118
            self.footerLabel.text = hasContacted ? "You've already contacted \(self.contact.name)." : "Enter your call result to get the next call."
        }
        self.resultNextButton.isHidden = hasCompleted || !hasContacted
        self.resultUnavailableButton.isHidden = hasCompleted || hasContacted
        self.resultVoicemailButton.isHidden = hasCompleted || hasContacted
        self.resultContactedButton.isHidden = hasCompleted || hasContacted
        self.resultSkipButton.isHidden = hasCompleted || hasContacted
        
        footer.setNeedsUpdateConstraints()
    }
    
    func callButtonPressed(_ button: UIButton) {
        callNumber(contact.phone)
    }

    func callNumber(_ number: String) {
        print("dialing \(number)")
        if let dialURL = URL(string: "telprompt:\(number)") {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(dialURL) { success in
                    //Log the result
                }
            } else {
                UIApplication.shared.openURL(dialURL)
            }
        }
    }
    
    func reportCallOutcome(_ log: ContactLog) {
        if log.outcome.characters.count > 0 {
            logs.add(log: log)
            let operation = ReportOutcomeOperation(log:log)
            OperationQueue.main.addOperation(operation)
        }
    }
    
    @IBAction func resultButtonPressed(_ button: UIButton) {
        var outcomeType = ""
        switch button {
        case resultContactedButton:
            outcomeType = "contacted"
            break
        case resultSkipButton:
            outcomeType = "skip"
            break
        case resultVoicemailButton:
{'fraction_non_alphanumeric': 0.08317605823672149, 'fraction_numerical': 0.004987867349689944, 'mean_word_length': 2.58752417794971, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3394992', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1976, 'n_words': 475}