//
//  AgoraAudioCallVC.swift
//  WoWonder
//
//  Created by iMac on 03/10/23.
//  Copyright © 2023 ScriptSun. All rights reserved.
//

import UIKit
import AgoraRtcKit
import SDWebImage
import Async
import Toast_Swift
import WowonderMessengerSDK
import AVFoundation

class AgoraAudioCallVC: BaseVC {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var descriptionLabel: UILabel!
    @IBOutlet weak var userImageView: UIImageView!
    
    // MARK: - Properties
    
    var incomingCallData: UserCallData?
    var outgoingCallData: AgoraCallSuccessModel?
    var userData: UserData?
    var room_name = ""
    var call_id = ""
    var agoraKit: AgoraRtcEngineKit!
    private var checkCallTimer = Timer()
    private var callOutgoingCount = 0
    private var callDeclinedTimer = Timer()
    private var callAudioPlayer: AVAudioPlayer?
    private var callTimer = Timer()
    private var seconds = 0
    private var isJoined = false
    
    // MARK: - View Life Cycles

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Back Button Action
    @IBAction func backButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.declineCall(call_id: self.call_id)
    }
    
    // Audio Mute Button Action
    @IBAction func audioMuteButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        sender.isSelected = !sender.isSelected
        self.agoraKit.muteLocalAudioStream(sender.isSelected)
    }
    
    // Speaker Button Action
    @IBAction func speakerButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        sender.isSelected = !sender.isSelected
        self.agoraKit.setEnableSpeakerphone(sender.isSelected)
    }
    
    // Hangup Button Action
    @IBAction func hangupButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.declineCall(call_id: self.call_id)
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.setData()
        self.initializeAgoraEngine()
        self.joinChannel()
    }
    
    // Set Data
    func setData() {
        if let callData = self.incomingCallData {
            let url = URL(string: callData.avatar ?? "")
            let indicator = SDWebImageActivityIndicator.medium
            self.userImageView.sd_imageIndicator = indicator
            DispatchQueue.global(qos: .userInteractive).async {
                self.userImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "dm_image"))
            }
            self.userNameLabel.text = callData.name ?? ""
            self.descriptionLabel.text = "Waiting for answer"
        } else {
            self.playCallSound()
            self.checkCallTimer = Timer.scheduledTimer(timeInterval: 0.6, target: self, selector: #selector(self.checkOutgoingCall), userInfo: nil, repeats: true)
            let url = URL(string: self.userData?.avatar ?? "")
            let indicator = SDWebImageActivityIndicator.medium
            self.userImageView.sd_imageIndicator = indicator
            DispatchQueue.global(qos: .userInteractive).async {
                self.userImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "dm_image"))
            }
            self.userNameLabel.text = self.userData?.name ?? ""
            self.descriptionLabel.text = "Calling"
        }
    }
    
    func initializeAgoraEngine() {
        let config = AgoraRtcEngineConfig()
        config.appId = AppInstance.instance.siteSetting?.agora_chat_app_id
        self.agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
    }
    
    func joinChannel() {
        if let token = self.incomingCallData?.data?.access_token {
            self.agoraKit.joinChannel(byToken: token, channelId: self.room_name, info: nil, uid: 0) { (channel, uid, elapsed) in
                self.agoraKit.setEnableSpeakerphone(true)
                UIApplication.shared.isIdleTimerDisabled = true
            }
        } else {
            self.agoraKit.joinChannel(byToken: self.outgoingCallData?.token, channelId: self.room_name, info: nil, uid: 0) { (channel, uid, elapsed) in
                self.agoraKit.setEnableSpeakerphone(true)
                UIApplication.shared.isIdleTimerDisabled = true
            }
        }
    }
    
    func leaveChannel() {
        if self.agoraKit != nil {
            self.agoraKit.leaveChannel(nil)
            UIApplication.shared.isIdleTimerDisabled = false
            self.agoraKit = nil
        }
    }
    
    private func onAnswerCall() {
        self.isJoined = true
        self.invalidateCallTimers()
        self.stopCallSound()
        self.callTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.setCallTimerToLabel), userInfo: nil, repeats: true)
        if self.incomingCallData == nil {
            self.setCallLog(id: String(self.outgoingCallData?.id ?? 0), user_id: self.userData?.user_id, avatar: self.userData?.avatar, name: self.userData?.name, from_id: AppInstance.instance.userId, active: self.userData?.active, time: Date().timeStamp(), status: "Outgoing", room_name: self.outgoingCallData?.room_name, call_type: "audio", type: "Answered")
        }
    }
    
    @objc private func setCallTimerToLabel() {
        self.seconds += 1
        self.descriptionLabel.text = self.getSecondsString()
    }
    
    private func getSecondsString() -> String {
        let hours = self.seconds / 3600
        let minutes = self.seconds / 60 % 60
        let seconds = self.seconds % 60
        return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
    }
    
    private func invalidateCallTimers() {
        self.checkCallTimer.invalidate()
        self.callDeclinedTimer.invalidate()
    }
    
    private func closeScreen() {
        self.invalidateCallTimers()
        self.callTimer.invalidate()
        self.stopCallSound()
        self.navigationController?.popToRootViewController(animated: true)
    }
    
    @objc private func checkOutgoingCall() {
        self.checkForCallAction(call_id: self.call_id)
    }
    
    @objc private func incrementOutgoingCallDeclinedCount() {
        self.callOutgoingCount += 1
    }
    
    private func playCallSound() {
        guard let url = Bundle.main.url(forResource: "mystic_call", withExtension: "mp3") else { return }
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
            try AVAudioSession.sharedInstance().setActive(true)
            self.callAudioPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
            guard let aPlayer = callAudioPlayer else { return }
            aPlayer.play()
        } catch let error {
            print(error.localizedDescription)
        }
    }
    
    private func stopCallSound() {
        self.callAudioPlayer?.stop()
    }
    
}

// MARK: - Extensions

// MARK: Api Call
extension AgoraAudioCallVC {
    
    func checkForCallAction(call_id: String) {
        if Connectivity.isConnectedToNetwork() {
            guard self.callOutgoingCount < 6 else {
                self.declineCall(call_id: call_id)
                return
            }
            Async.background {
                CallManager.instance.checkForAgoraCall(access_token: AppInstance.instance._sessionId, call_id: call_id) { (success, sessionError, serverError, error) in
                    if let success = success {
                        Async.main {
                            self.dismissProgressDialog {
                                switch success._callStatusObject {
                                case .answered:
                                    self.invalidateCallTimers()
                                case .declined, .no_answer:
                                    print("declined")
                                    if self.incomingCallData == nil {
                                        self.setCallLog(id: String(self.outgoingCallData?.id ?? 0), user_id: self.userData?.user_id, avatar: self.userData?.avatar, name: self.userData?.name, from_id: AppInstance.instance.userId, active: self.userData?.active, time: Date().timeStamp(), status: "Missing", room_name: self.outgoingCallData?.room_name, call_type: "audio", type: "NoAnswer")
                                    }
                                    self.leaveChannel()
                                    self.closeScreen()
                                    break
                                case .calling:
                                    if !self.callDeclinedTimer.isValid {
                                        self.callDeclinedTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.incrementOutgoingCallDeclinedCount), userInfo: nil, repeats: true)
                                    }
                                    break
                                case .unknown:
                                    break
                                }
                            }
                        }
                    } else if sessionError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                self.view.makeToast(sessionError?.errors?.error_text)
                            }
                        }
                    } else if serverError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                self.view.makeToast(serverError?.errors?.error_text)
                            }
                        }
                    } else {
                        Async.main {
                            self.dismissProgressDialog {
                                self.view.makeToast(error?.localizedDescription)
                            }
                        }
                    }
                }
            }
        } else {
            self.dismissProgressDialog {
                self.view.makeToast(InterNetError)
            }
        }
    }
    
    private func declineCall(call_id: String) {
        if Connectivity.isConnectedToNetwork() {
            Async.background {
                CallManager.instance.agoraCallAction(access_token: AppInstance.instance._sessionId, call_id: call_id, action: "decline") { (success, sessionError, serverError, error) in
                    if success != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                if self.incomingCallData == nil {
                                    if !self.isJoined {
                                        /*self.setCallLog(id: String(self.outgoingCallData?.id ?? 0), user_id: self.userData?.user_id, avatar: self.userData?.avatar, name: self.userData?.name, from_id: AppInstance.instance.userId, active: self.userData?.active, time: Date().timeStamp(), status: "Outgoing", room_name: self.outgoingCallData?.room_name, call_type: "audio", type: "Cancel")
                                    } else {*/
                                        self.setCallLog(id: String(self.outgoingCallData?.id ?? 0), user_id: self.userData?.user_id, avatar: self.userData?.avatar, name: self.userData?.name, from_id: AppInstance.instance.userId, active: self.userData?.active, time: Date().timeStamp(), status: "Missing", room_name: self.outgoingCallData?.room_name, call_type: "audio", type: "NoAnswer")
                                    }
                                }
                                self.leaveChannel()
                                self.closeScreen()
                            }
                        }
                    } else if sessionError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                self.view.makeToast(sessionError?.errors?.error_text)
                            }
                        }
                    } else if serverError != nil {
                        Async.main {
                            self.dismissProgressDialog {
                                self.view.makeToast(serverError?.errors?.error_text)
                            }
                        }
                    } else {
                        Async.main {
                            self.dismissProgressDialog {
                                self.view.makeToast(error?.localizedDescription)
                            }
                        }
                    }
                }
            }
        } else {
            self.dismissProgressDialog {
                self.view.makeToast(InterNetError)
            }
        }
    }
    
}

// MARK: AgoraRtcEngineDelegate Methods
extension AgoraAudioCallVC: AgoraRtcEngineDelegate {
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
        self.onAnswerCall()
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid: UInt, reason: AgoraUserOfflineReason) {
        self.leaveChannel()
        self.closeScreen()
    }
    
}
