//
//  ReplyViewController.swift
//  Kalker

import UIKit
import Alamofire
import SwiftyJSON

protocol ResetList {
    func idLastReset(data: Int)
}

class ReplyViewController: UIViewController {

    var name: String!
    var avatar: UIImage!
    var callId: Int!
    var rowId: Int!
    var patientId: Int!
    var caregiverId: Int!
    var patientPhone: String!
    
    let callsTitle = Call.getCallTitle()
    let callsImage = Call.getCallImage()
    
    var delegate: ResetList? = nil
    
    @IBOutlet weak var popup: UIView!
    @IBOutlet weak var popupViewForBtns: UIView!
    @IBOutlet weak var popupAvatar: UIImageView!
    @IBOutlet weak var popupName: UILabel!
    @IBOutlet weak var popupCall: UILabel!
    @IBOutlet weak var popupCallImage: UIImageView!
    @IBOutlet weak var popupTime: UILabel!
    @IBOutlet weak var popupStepper: UIStepper!
    @IBOutlet weak var minLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        setWhiteBackground(viewForBackground: popup)
        setDefaultBackground(viewForBackground: popupViewForBtns)
        popup.layer.cornerRadius = 5
        popup.layer.masksToBounds = true
        popupAvatar.setRoundImage()
        popupCallImage.layer.cornerRadius = 7
        popupCallImage.layer.masksToBounds = true
        
        popupName.text = name
        popupAvatar.image = avatar
        popupCall.text = self.callsTitle[callId]
        popupCallImage.image = self.callsImage[callId]
        minLabel.isHidden = true
    }
    
    @IBAction func changeTime(_ sender: UIStepper) {
        if popupStepper.value == 0 {
            popupTime.text = "Teraz"
            minLabel.isHidden = true
        }
        else {
            popupTime.text = String(Int(popupStepper.value))
            minLabel.isHidden = false
        }
    }
    
    @IBAction func sendReply(_ sender: UIButton) {
        let parameters = ["parameter": 1,
                          "id": rowId,
                          "idPatient": self.patientId!,
                          "idCaregiver": self.caregiverId!,
                          "reply": popupStepper.value] as [String : Any]
        
        Alamofire.request("https://kalkerapp.pl/api/replyCall.php", method: .post, parameters: parameters)
            .responseJSON { response in
                if let jsonData = response.result.value {
                    let json = JSON(jsonData)
                    print(json)
                    print("Send Reply")
                }
        }
        
        dismiss(animated: true, completion: {
            self.delegate?.idLastReset(data: 0)
        })
    }
    
    @IBAction func callToPatient(_ sender: UIButton) {
        //dismiss(animated: true, completion: nil)
        
        let alert = UIAlertController(title: "Kalker", message: "Czy na pewno chcesz zadzwonić do \(name!)?", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Anuluj", style: .cancel, handler: nil))
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
            switch action.style{
            case .default:
                let parameters = ["parameter": 2,
                                  "id": self.rowId,
                                  "idPatient": self.patientId!,
                                  "idCaregiver": self.caregiverId!,
                                  "reply": self.popupStepper.value] as [String : Any]
                
                Alamofire.request("https://kalkerapp.pl/api/replyCall.php", method: .post, parameters: parameters)
                    .responseJSON { response in
                        if let jsonData = response.result.value {
                            let json = JSON(jsonData)
                            print(json)
                            print("Send Reply")
                        }
                }
                
                self.dismiss(animated: true, completion: {
                    self.delegate?.idLastReset(data: 0)
                })
                
                guard let number = URL(string: "telprompt://\(self.patientPhone!)") else { return }
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(number)
                } else {
                    UIApplication.shared.openURL(number)
                }
                
            case .cancel:
                print("cancel")
                
            case .destructive:
                print("destructive")
            }
        }))
        self.present(alert, animated: true, completion: nil)
    }

    @IBAction func closePopup(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
