package com.zxhl.main.page.activity

import android.content.Intent
import android.util.Log
import android.view.View
import com.facebook.*
import com.facebook.login.LoginConfiguration
import com.facebook.login.LoginManager
import com.facebook.login.LoginResult
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.Constant
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.utils.*
import com.zxhl.cms.widget.LoadingDialog
import com.zxhl.main.R
import kotlinx.android.synthetic.main.activity_layout_third_party_login.*
import java.util.*


class ThirdpartyLoginActivity : BaseActivity(), WeChatLoginSuccessCallBall {
    //    private var wechatutil: WeChatUtils? = null
    private var mLoading: LoadingDialog? = null
    override fun onClick(v: View?) {

    }

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    override fun layoutID(): Int {
        return R.layout.activity_layout_third_party_login
    }

    private var googleLogin: GoogleLoginUtil? = null;
    private var faceBookLogin: FaceBookLoginUtil? = null;

    override fun init() {
        mLoading = LoadingDialog.getLoadingDialog(
            this,
            getString(R.string.wait_ing),
            false,
            false
        )
//        wechatutil = WeChatUtils(this, this)

        //googleLogin init
        googleLogin = GoogleLoginUtil(this)
        googleLogin?.init()
        //FaceBook
        faceBookLogin = FaceBookLoginUtil(this)
        faceBookLogin?.init()

        id_ll_google_login?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                EventUtils.onEvent("googleLoginClick")
                mLoading?.setLoading(getString(R.string.wait_ing2))
                mLoading?.show()
                googleLogin?.googleLogin(object : GoogleLoginUtil.LoginCallBack {
                    override fun onLoginSuccess() {
                        runOnUiThread {
                            Constant.NewUserBox.isRefreshHomeList = true
                            mLoading?.setResult(true, "login success", 0)
                            finish()
                        }
                    }

                    override fun onLoginError(errorMsg: String) {
                        EventUtils.onEvent("GoogleLoginError", errorMsg)
                        runOnUiThread {
                            mLoading?.setResult(false, "login error", 1000)
                        }
                    }
                })
            }
        })
        id_ll_facebook_login?.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                EventUtils.onEvent("facebookLoginClick")
                mLoading?.setLoading(getString(R.string.wait_ing2))
                mLoading?.show()
                faceBookLogin?.faceBookLogin(object : FaceBookLoginUtil.LoginCallBack {
                    override fun onLoginSuccess() {
                        Constant.NewUserBox.isRefreshHomeList = true
                        runOnUiThread {
                            mLoading?.setResult(true, "login success", 0)
                            finish()
                        }
                    }

                    override fun onLoginError(errorMsg: String) {
                        if (errorMsg == "Success_未获取到登录信息"){
                            runOnUiThread {
                                mLoading?.setResult(false, "try again", 1000)
                            }
                        }else{
                            runOnUiThread {
                                mLoading?.setResult(false, "login error", 1000)
                            }
                        }
                        EventUtils.onEvent("facebookLoginError", errorMsg)
//                        Log.e("FaceBookLoginUtil", "error $errorMsg")
                    }
                })
            }
        })
    }
    override fun loginSuccess(lottery: String?) {
        this.finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        faceBookLogin?.onActivityResult(requestCode, resultCode, data)
        googleLogin?.onActivityResult(requestCode, resultCode, data)
    }

}