package com.develop.common.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.fragment.app.FragmentManager
import com.develop.base.mvvm.FullScreenTransparentDialog
import com.develop.base.util.ThreadUtils
import com.develop.base.util.TopResumedAtyHolder
import com.develop.common.R
import com.develop.common.databinding.DialogRecipeUpdateBinding

class RecipeUpdateDialog() : FullScreenTransparentDialog() {

    lateinit var binding: DialogRecipeUpdateBinding

    lateinit var onDialogClickListener: OnDialogClickListener

    var type = "UPDATE_TIPS";

    var msg = ""

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = DialogRecipeUpdateBinding.inflate(
            inflater, container, false
        )

        binding.tvYes.setOnClickListener {
            onDialogClickListener.onConfirm()
            removeSelf()
        }

        binding.tvCancel.setOnClickListener {
            onDialogClickListener.onCancel()
            removeSelf()
        }

        if (type == "UPDATE_TIPS") {
            binding.tvMsg.text = msg
            binding.tvCancel.visibility = View.VISIBLE;
            binding.tvYes.visibility = View.VISIBLE;
        }

        if (type == "NO_UPDATE") {
            binding.tvMsg.text = msg

            binding.tvCancel.visibility = View.GONE;
            binding.tvYes.visibility = View.VISIBLE;
            val layoutParams = binding.tvYes.layoutParams as RelativeLayout.LayoutParams;
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
            binding.tvCancel.layoutParams = layoutParams
            binding.tvYes.text = getString(R.string.ok)
        }

        if (type == "UPDATING") {
            binding.tvMsg.text = msg
            binding.tvCancel.visibility = View.GONE;
            binding.tvYes.visibility = View.VISIBLE;
            binding.loading.visibility = View.VISIBLE;
            binding.success.visibility = View.GONE;
            val layoutParams = binding.tvYes.layoutParams as RelativeLayout.LayoutParams;
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
            binding.tvCancel.layoutParams = layoutParams
            binding.tvYes.text = getString(R.string.stop)
        }

        if (type == "SUCCESS") {
            binding.tvMsg.text = msg
            binding.tvCancel.visibility = View.GONE;
            binding.tvYes.visibility = View.VISIBLE;
            binding.loading.visibility = View.GONE;
            binding.success.visibility = View.VISIBLE;
            val layoutParams = binding.tvYes.layoutParams as RelativeLayout.LayoutParams;
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
            binding.tvCancel.layoutParams = layoutParams
            binding.tvYes.text = getString(R.string.ok)
        }

        return binding.root
    }

    fun showNoUpdateTips(manager: FragmentManager, tag: String) {
        TopResumedAtyHolder.getCurrentActivity()?.apply {
            msg = getString(R.string.no_recipe_update_tips)
        }
        type = "NO_UPDATE"
        showDialog(manager,tag)
    }

    fun showUpdating(
        manager: FragmentManager, tag: String, current: Long, total: Long
    ) {

        TopResumedAtyHolder.getCurrentActivity()?.apply {
            msg = getString(R.string.recipe_update_process_tips).replace(
                "{{progress}}",
                "${current}/${total}"
            )
        }
        if (isAdded) {
            ThreadUtils.runOnMainThread {
                dialog?.hide()
                binding.tvMsg.text = msg
                dialog?.show()
            }
        } else {
            type = "UPDATING"
            showDialog(manager, tag)
        }


    }

    fun showSuccess(
        manager: FragmentManager, tag: String
    ) {
        TopResumedAtyHolder.getCurrentActivity()?.apply {
            msg = getString(R.string.recipe_update_finish)

        }
        type = "SUCCESS"
        showDialog(manager, tag)
    }

    fun showUpdateTips(
        manager: FragmentManager, tag: String, num: Long
    ) {
        TopResumedAtyHolder.getCurrentActivity()?.apply {
            msg = getString(R.string.recipe_update_tips).replace("{{num}}", num.toString())
        }
        type = "UPDATE_TIPS"
        showDialog(manager, tag)
    }


    interface OnDialogClickListener {
        fun onConfirm()
        fun onCancel()
    }

}