package com.pan_american.android.util

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import com.pan_american.android.R

class CommitButton(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {

    companion object {
        const val STATUS_IN_COMMITING = 1
        const val STATUS_COMMITTED = 0
    }

    private val view: View = LayoutInflater.from(context).inflate(R.layout.layout_commit_button, this)

    private val commitButton: LinearLayout = view.findViewById(R.id.commit_button)

    private val commitText: TextView = view.findViewById(R.id.commit_text)

    private val commitLoading: ProgressBar = view.findViewById(R.id.commit_loading)

    private var buttonStatus = 0

    fun setText(text: String) {
        commitText.text = text
    }

    fun setButtonStatus(type: Int) {
        when(type) {
            STATUS_IN_COMMITING -> {

                if (buttonStatus == 0) {
                    commitText.visibility = View.GONE

                    commitLoading.visibility = View.VISIBLE

                    commitButton.isEnabled = false
                }

            }

            STATUS_COMMITTED -> {

                if (buttonStatus == 1) {
                    commitText.visibility = View.VISIBLE

                    commitLoading.visibility = View.GONE

                    commitButton.isEnabled = true
                }

            }
        }

        buttonStatus = type
    }
}