import UIKit
import Async
import WowonderMessengerSDK
import Toast_Swift

enum PrivacyAlertType {
    case Follow_me
    case Message_me
    case Birthday
}

protocol PrivacyAlertVCDelegate {
    func handlePrivacyTap()
}

class PrivacyAlertVC: BaseVC {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var everyoneView: UIView!
    @IBOutlet weak var peopleIFollowView: UIView!
    @IBOutlet weak var noBodyView: UIView!
    @IBOutlet weak var everyoneButton: UIButton!
    @IBOutlet weak var peopleIFollowButton: UIButton!
    @IBOutlet weak var noBodyButton: UIButton!
    
    // MARK: - Properties
    
    var titleText = ""
    var alertType: PrivacyAlertType = .Follow_me
    var delegate: PrivacyAlertVCDelegate?
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Everyone Button Action
    @IBAction func everyoneButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        switch self.alertType {
        case .Follow_me:
            self.updateFollowPrivacy(privacy: 0)
        case .Message_me:
            self.updateMessagePrivacy(privacy: 0)
        case .Birthday:
            self.updateBirthdayPrivacy(privacy: 0)
        }
    }
    
    // People I Follow Button Action
    @IBAction func peopleIFollowButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        switch alertType {
        case .Follow_me:
            self.updateFollowPrivacy(privacy: 1)
        case .Message_me:
            self.updateMessagePrivacy(privacy: 1)
        case .Birthday:
            self.updateBirthdayPrivacy(privacy: 1)
        }
    }
    
    // No Body Button Action
    @IBAction func noBodyButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        switch alertType {
        case .Follow_me:
            break
        case .Message_me:
            self.updateMessagePrivacy(privacy: 2)
        case .Birthday:
            self.updateBirthdayPrivacy(privacy: 2)
        }
    }
    
    // Close Button Action
    @IBAction func closeButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.dismiss(animated: true, completion: nil)
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.view.backgroundColor = .black.withAlphaComponent(0.4)
        self.setTitle()
    }
    
    // Set Title
    func setTitle() {
        self.titleLabel.text = self.titleText
    }
    
}

// MARK: - Extensions

// MARK: Api Call
extension PrivacyAlertVC {
    
    private func updateFollowPrivacy(privacy: Int) {
        if Connectivity.isConnectedToNetwork() {
            let sessionToken = AppInstance.instance.sessionId ?? ""
            Async.background {
                PrivacyManager.instance.updateFollowPrivacy(session_Token: sessionToken, privacy: privacy, completionBlock: { (success, sessionError, serverError, error) in
                    if success != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                print("success = \(success?.message ?? "")")
                                // self.view.makeToast(success?.message ?? "")
                                AppInstance.instance.fetchUserProfile { _ in
                                    self.dismiss(animated: true) {
                                        self.delegate?.handlePrivacyTap()
                                    }
                                }
                            }
                        }
                    } else if sessionError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                print("sessionError = \(sessionError?.errors?.error_text ?? "")")
                                self.view.makeToast(sessionError?.errors?.error_text ?? "")
                            }
                        }
                    } else if serverError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                print("serverError = \(serverError?.errors?.error_text ?? "")")
                                self.view.makeToast(serverError?.errors?.error_text ?? "")
                            }
                        }
                    } else {
                        Async.main {
                            self.dismissProgressDialog {
                                print("error = \(error?.localizedDescription ?? "")")
                                self.view.makeToast(error?.localizedDescription ?? "")
                            }
                        }
                    }
                })
            }
        } else {
            self.dismissProgressDialog {
                self.view.makeToast(InterNetError)
            }
        }
    }
    
    private func updateMessagePrivacy(privacy: Int) {
        if Connectivity.isConnectedToNetwork() {
            let sessionToken = AppInstance.instance.sessionId ?? ""
            Async.background {
                PrivacyManager.instance.updateMessagePrivacy(session_Token: sessionToken, privacy: privacy, completionBlock: { (success, sessionError, serverError, error) in
                    if success != nil{
                        Async.main {
                            self.dismissProgressDialog {
                                print("success = \(success?.message ?? "")")
                                // self.view.makeToast(success?.message ?? "")
                                AppInstance.instance.fetchUserProfile { _ in
                                    self.dismiss(animated: true) {
                                        self.delegate?.handlePrivacyTap()
                                    }
                                }
                            }
                        }
                    } else if sessionError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                print("sessionError = \(sessionError?.errors?.error_text ?? "")")
                                self.view.makeToast(sessionError?.errors?.error_text ?? "")
                            }
                        }
                    } else if serverError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                print("serverError = \(serverError?.errors?.error_text ?? "")")
                                self.view.makeToast(serverError?.errors?.error_text ?? "")
                            }
                        }
                    } else {
                        Async.main {
                            self.dismissProgressDialog {
                                print("error = \(error?.localizedDescription ?? "")")
                                self.view.makeToast(error?.localizedDescription ?? "")
                            }
                        }
                    }
                })
            }
        } else {
            self.dismissProgressDialog {
                self.view.makeToast(InterNetError)
            }
        }
    }
    
    func updateBirthdayPrivacy(privacy: Int) {
        if Connectivity.isConnectedToNetwork() {
            let sessionToken = AppInstance.instance.sessionId ?? ""
            Async.background {
                PrivacyManager.instance.updateBirthPrivacy(session_Token: sessionToken, privacy: privacy) { (success, sessionError, serverError, error) in
                    if success != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                print("success = \(success?.message ?? "")")
                                // self.view.makeToast(success?.message ?? "")
                                AppInstance.instance.fetchUserProfile { _ in 
                                    self.dismiss(animated: true) {
                                        self.delegate?.handlePrivacyTap()
                                    }
                                }
                            }
                        }
                    } else if sessionError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                print("sessionError = \(sessionError?.errors?.error_text ?? "")")
                                self.view.makeToast(sessionError?.errors?.error_text ?? "")
                            }
                        }
                    } else if serverError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                print("serverError = \(serverError?.errors?.error_text ?? "")")
                                self.view.makeToast(serverError?.errors?.error_text ?? "")
                            }
                        }
                    } else {
                        Async.main {
                            self.dismissProgressDialog {
                                print("error = \(error?.localizedDescription ?? "")")
                                self.view.makeToast(error?.localizedDescription ?? "")
                            }
                        }
                    }
                }
            }
        }
    }
    
}
