//
//  GroupViewController.swift
//  WoWonder
//
//  Created by UnikaApp on 14/10/22.
//  Copyright © 2022 ScriptSun. All rights reserved.
//

import Foundation
import Async
import UIKit
import Toast_Swift

extension ChatUserListVC {
    
    // Get Group Chat Data List
    func getGroupChatDataList(_ completion: @escaping (FetchGroupModel) -> Void) {
        if self.groupfetchSatus {
            groupfetchSatus = false
        } else {
            print("will not show Hud more...")
        }
        let sessionToken = AppInstance.instance.sessionId ?? ""
        Async.background {
            GroupChatManager.instance.fetchGroups(session_Token: sessionToken, type: "get_list", limit: 10, completionBlock: { (success, sessionError, serverError, error) in
                if success != nil {
                    Async.main {
                        self.dismissProgressDialog {
                            print("userList = \(success?.data ?? [])")
                            self.groupsArray = success?.data ?? []
                            guard let success = success else { return }
                            completion(success)
                        }
                    }
                } else if sessionError != nil {
                    Async.main {
                        self.dismissProgressDialog {
                            self.view.makeToast(sessionError?.errors?.error_text ?? "")
                            print("sessionError = \(sessionError?.errors?.error_text ?? "")")
                        }
                    }
                } else if serverError != nil {
                    Async.main {
                        self.dismissProgressDialog {
                            self.view.makeToast(serverError?.errors?.error_text)
                            print("serverError = \(serverError?.errors?.error_text ?? "")")
                        }
                    }
                } else {
                    Async.main {
                        self.dismissProgressDialog {
                            self.view.makeToast(error?.localizedDescription)
                            print("error = \(error?.localizedDescription ?? "")")
                        }
                    }
                }
            })
        }
    }
    
}
