package com.ym.modulecommon.utils

import android.app.Activity
import android.app.Dialog
import android.text.Html
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.ym.admodule.R
import com.ym.modulecommon.AppliContext
import com.ym.modulecommon.base.BaseDialog
import com.ym.modulecommon.news.entity.AdNewsHomeEntity

object AdWebDialog : BaseDialog() {

    /**
     * 退出弹窗
     */
    fun showSignOutDialog(
        activity: Activity,
        isComplete: Boolean,
        taskNum:Int,
        nextListener: View.OnClickListener,
        cancelListener: View.OnClickListener
    ) {
        if (activity == null) {
            return
        }
//        Constant.isDialogActivity = 1
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            showBottomDialog(R.layout.dialog_sign_out_layout, activity, mDialog, 0, Gravity.CENTER)
        val mInfo = mDialogView.findViewById<TextView>(R.id.id_ad_dialog_info)
        val btn = mDialogView.findViewById<TextView>(R.id.dialog_scratch_dialog_bg)
        val cancelBtn = mDialogView.findViewById<TextView>(R.id.dialog_scratch_dialog_cancel)

        var info = ""
        var btnText = ""
        var canceBtnText = ""
        if (isComplete) {
            info = "恭喜你完成任务！再完成${taskNum}条任务， 即可领取秒到账微信红包"
            btnText = "继续做下一条"
            canceBtnText = "一会再来"
        } else {
            info = "还未达到任务要求，现在退出将无 法获得奖励，确定要退出么？"
            btnText = "继续领取奖励"
            canceBtnText = "仍要退出"
        }
        mInfo?.text = Html.fromHtml("${info}")
        btn?.text = btnText
        cancelBtn?.text = canceBtnText

        btn?.setOnClickListener {
            nextListener.onClick(it)
            mDialog?.dismiss()
        }
        cancelBtn?.setOnClickListener {
            cancelListener.onClick(it)
            mDialog?.dismiss()
            activity?.finish()
        }
        mDialog?.setCancelable(false)
        DialogUtils.show(mDialog)
    }

    /**
     * 首页奖励
     */
    fun showHomeReward(activity: Activity, result: AdNewsHomeEntity,listener:View.OnClickListener) {
        if (activity == null) {
            return
        }
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            showBottomDialog(R.layout.dialog_homr_reward, activity, mDialog, 0, Gravity.CENTER)

        val mRewardNum = mDialogView.findViewById<TextView>(R.id.id_dialog_reward_num)
        val mGuangImg = mDialogView.findViewById<ImageView>(R.id.iv_dialog_guang_img)
        val mRewardImg = mDialogView.findViewById<ImageView>(R.id.iv_dialog_reward_img)
        val mWithdrawNum = mDialogView.findViewById<TextView>(R.id.id_dialog_withdraw_num)
        val layoutDialogBtn = mDialogView.findViewById<RelativeLayout>(R.id.layout_dialog_btn)

        mRewardNum?.text = Html.fromHtml("恭喜你获得<font color=\"#E9373A\">${result?.awardNum}个${result?.unit}</font>")

        if (result?.isWithdrawal == 1) {
            mWithdrawNum?.text = "可提现"
        } else {
            mWithdrawNum?.text = "还差${result?.totalNumber ?: 0 - result.completeNumber!! ?: 0}个即可提现"
        }

        mGuangImg?.animation = AnimationUtils.loadAnimation(activity,R.anim.rotate_anim)
        if (!TextUtils.isEmpty(result?.currencyIcon) && result?.currencyIcon?.contains("http")!!) {
            Glide.with(AppliContext.get()).load(result?.currencyIcon)
                .error(R.drawable.icon_ad_news_home_fu_big).into(mRewardImg)
        } else {
            mRewardImg?.setImageResource(R.drawable.icon_ad_news_home_fu_big)
        }

        layoutDialogBtn?.setOnClickListener {
            mDialog?.dismiss()
            listener?.onClick(it)
        }

        mDialog?.setCancelable(false)
        DialogUtils.show(mDialog)
    }

}