package com.yidian.bcommon.utils

import android.app.Activity
import com.umeng.socialize.UMAuthListener
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.bean.SHARE_MEDIA
import com.yidian.utils.ToastUtil


/**
 * 授权登录工具类
 */
object AuthUtils {

  /**
   * 校验是否已经授权
   */
  fun checkIsAuth(activity: Activity, platform: SHARE_MEDIA): Boolean {
    return UMShareAPI.get(activity).isAuthorize(activity, platform)
  }

  /**
   * 进行平台授权
   *
   * @return 返回授权信息的map
   */
  fun toAuthorize(activity: Activity, platform: SHARE_MEDIA): Map<String, String> {
    var authMap: Map<String, String> = HashMap()
    UMShareAPI.get(activity).getPlatformInfo(activity, platform, object : UMAuthListener {

      override fun onStart(share_media: SHARE_MEDIA) {}

      override fun onComplete(share_media: SHARE_MEDIA, i: Int, map: Map<String, String>) {
        authMap = map
      }

      override fun onError(share_media: SHARE_MEDIA, i: Int, throwable: Throwable) {
        ToastUtil.showToast(activity, throwable.message)
      }

      override fun onCancel(share_media: SHARE_MEDIA, i: Int) {
        ToastUtil.showToast(activity, "用户已取消")
      }
    })
    return authMap
  }
}
