package com.develop.food.base.widgets

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
import com.develop.base.R
import com.develop.base.databinding.DialogRecipeUpdateBinding
import com.develop.food.base.ui.FullScreenTransparentDialog
import com.develop.food.base.utils.ThreadUtils
import com.develop.food.base.utils.TopResumedActivityHolder

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, context: AppCompatActivity? = TopResumedActivityHolder.getCurrentActivity()) {

        if (context != null) {
            msg = context.getString(R.string.no_recipe_update_tips)
        }
        this.type = "NO_UPDATE";
        this.showDialog(manager, tag)
    }

    fun showUpdating(manager: FragmentManager, tag: String, current: Long, total: Long, context: AppCompatActivity? = TopResumedActivityHolder.getCurrentActivity()) {
        if (context != null) {
            msg = context.getString(R.string.recipe_update_process_tips).replace("{{progress}}", "${current}/${total}")
        }

        if (this.isAdded) {
            ThreadUtils.runOnMainThread {
                this.dialog?.hide()
                this.binding.tvMsg.text = msg;
                this.dialog?.show()
            }
        } else {
            this.type = "UPDATING"
            this.showDialog(manager, tag)
        }


    }

    fun showSuccess(manager: FragmentManager, tag: String, context: AppCompatActivity? = TopResumedActivityHolder.getCurrentActivity()) {
        if (this.isAdded) {

        } else {
            if (context != null) {
                msg = context.getString(R.string.recipe_update_finish)
            }
            this.type = "SUCCESS";
            this.showDialog(manager, tag)
        }
    }

    fun showUpdateTips(manager: FragmentManager, tag: String, num: Long, context: AppCompatActivity? = TopResumedActivityHolder.getCurrentActivity()) {
        if (this.isAdded) {

        } else {
            if (context != null) {
                msg =  context.getString(R.string.recipe_update_tips).replace("{{num}}", num.toString())
            }
            this.type = "UPDATE_TIPS";
//            this.show(manager, tag)
            this.showDialog(manager,tag);
        }
    }


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

}