package com.miya.fastcashier.util

import android.content.Context
import android.widget.Toast
import es.dmoral.toasty.Toasty
import com.miya.fastcashier.util.CenterToasty
import android.widget.LinearLayout
import android.widget.TextView
import android.view.Gravity
import android.widget.ImageView
import androidx.annotation.CheckResult

/**
 * 居中的Toasty弹窗 将图片和文字的左右排布，改为了 上下排布
 * Created by fengyu on 2018/10/8.
 */
object CenterToasty {
    @CheckResult
    fun info(context: Context, message: CharSequence, duration: Int, withIcon: Boolean): Toast {
        val toast = Toasty.info(context, message, duration, withIcon)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun info(context: Context, message: CharSequence, duration: Int): Toast {
        val toast = Toasty.info(context, message, duration)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun info(context: Context, message: CharSequence): Toast {
        val toast = Toasty.info(context, message)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun infox(context: Context, message: CharSequence, xOffset: Int): Toast {
        val toast = Toasty.info(context, message)
        transformToast(toast, xOffset, 0)
        return toast
    }

    @CheckResult
    fun normal(context: Context, message: CharSequence, duration: Int): Toast {
        val toast = Toasty.normal(context, message, duration)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun warning(context: Context, message: CharSequence, duration: Int): Toast {
        val toast = Toasty.warning(context, message, duration)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun warning(context: Context, message: CharSequence, duration: Int, withIcon: Boolean): Toast {
        val toast = Toasty.warning(context, message, duration, withIcon)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun success(context: Context, message: CharSequence, duration: Int, withIcon: Boolean): Toast {
        val toast = Toasty.success(context, message, duration, withIcon)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun success(context: Context, message: CharSequence, duration: Int): Toast {
        val toast = Toasty.success(context, message, duration)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun error(context: Context, message: CharSequence, duration: Int, withIcon: Boolean): Toast {
        val toast = Toasty.error(context, message, duration, withIcon)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun error(context: Context, message: CharSequence): Toast {
        val toast = Toasty.error(context, message)
        transformToast(toast)
        return toast
    }

    @CheckResult
    fun error(context: Context, message: CharSequence, duration: Int): Toast {
        val toast = Toasty.error(context, message, duration)
        transformToast(toast)
        return toast
    }

    private fun transformToast(toast: Toast) {
        val linearLayout = toast.view as LinearLayout?
        linearLayout!!.orientation = LinearLayout.VERTICAL
        linearLayout.setPadding(20, 20, 20, 20)
        val icon = linearLayout.getChildAt(0) as ImageView
        icon.layoutParams = LinearLayout.LayoutParams(50, 50)
        val messageTextView = linearLayout.getChildAt(1) as TextView
        messageTextView.textSize = 28f
        messageTextView.setPadding(0, 30, 0, 30)
        messageTextView.gravity = Gravity.CENTER
        toast.setGravity(Gravity.CENTER, 0, 0)
    }

    private fun transformToast(toast: Toast, xOffset: Int, yOffset: Int) {
        val linearLayout = toast.view as LinearLayout?
        linearLayout!!.orientation = LinearLayout.VERTICAL
        linearLayout.setPadding(20, 20, 20, 20)
        val icon = linearLayout.getChildAt(0) as ImageView
        icon.layoutParams = LinearLayout.LayoutParams(50, 50)
        val messageTextView = linearLayout.getChildAt(1) as TextView
        messageTextView.textSize = 28f
        messageTextView.setPadding(0, 30, 0, 30)
        messageTextView.gravity = Gravity.CENTER
        toast.setGravity(Gravity.CENTER, xOffset, yOffset)
    }
}