//
//  SessionStore.swift
//  FirebaseAuthentification
//
//  Created by Sébastien Filaumart on 10/01/2020.
//  Copyright © 2020 Sébastien Filaumart. All rights reserved.
//

import SwiftUI
import Firebase
import Combine

class SessionStore: ObservableObject {
    var didChange = PassthroughSubject<SessionStore, Never>()
    @Published var session : User? {didSet {self.didChange.send(self)}}
    var handle : AuthStateDidChangeListenerHandle?
    
    func listen() {
        handle = Auth.auth().addStateDidChangeListener({ (auth, user) in
            if let user = user {
                self.session = User(uid : user.uid, email : user.email)
            } else {
                self.session = nil
            }
        })
    }
    
    func signUp(email : String, password : String, handle : @escaping AuthDataResultCallback){
        Auth.auth().createUser(withEmail: email, password: password, completion: handle)
    }
    
    func signIn(email : String, password : String, handle : @escaping AuthDataResultCallback){
        Auth.auth().signIn(withEmail: email, password: password, completion: handle)
    }
    
    func signOut() {
        do {
            try Auth.auth().signOut()
            self.session = nil
        } catch{
            print("Error signing out")
        }
    }
    
    func unbind()  {
        if let handle = handle{
            Auth.auth().removeStateDidChangeListener(handle)
        }
    }
        
    deinit{
        unbind()
    }

}

struct User {
        var uid : String
        var email : String?
        
    init(uid : String, email : String?) {
        self.uid = uid
        self.email = email
    }
}
