package com.swago.login

import android.content.Intent
import android.widget.Toast
import com.facebook.CallbackManager
import com.facebook.FacebookCallback
import com.facebook.FacebookException
import com.facebook.login.LoginManager
import com.facebook.login.LoginResult
import com.swago.baseswago.util.AppContext

/**
 *@date 2021/8/24 13:49
 *description:
 */
object FBLogin {
    private val manager = CallbackManager.Factory.create()
    fun init(iFacebook: IFacebook){
        LoginManager.getInstance().registerCallback(manager,object:FacebookCallback<LoginResult>{

            override fun onSuccess(result: LoginResult?) {
                result?.apply {
                    iFacebook.getToken(accessToken.token)
                }
            }

            override fun onCancel() {
                Toast.makeText(AppContext.getContext(), "Login Cancel", Toast.LENGTH_SHORT).show()
            }

            override fun onError(error: FacebookException?) {
                Toast.makeText(AppContext.getContext(), error?.message?:"", Toast.LENGTH_SHORT).show()
            }

        })
    }


    fun login(activity: LoginActivity){
        LoginManager.getInstance().logInWithReadPermissions(activity, listOf("public_profile"))
    }

    fun onActivityResult(requestCoe:Int, resultCode:Int, data: Intent){
        manager.onActivityResult(requestCoe,resultCode, data)
    }
}