package com.seatel.mobilehall.util

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.seatel.mobilehall.R
import com.seatel.mobilehall.ui.profile.adapter.BottomAccountChooserAdapter

@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class BottomAccountChooserDialog(context: Context) : Dialog(context) {

    private val recyclerView: RecyclerView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_bottom_chooser, null)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(view)
        if (this.window != null) {
            this.window!!.setDimAmount(0.5f)
            this.window!!.setLayout(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT
            )
            this.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            this.window!!.setGravity(Gravity.CLIP_VERTICAL)
            this.window!!.attributes.windowAnimations = R.style.DialogAnimationScaleInOut
            // this.window!!.attributes.windowAnimations = R.style.DialogAnimationSlideUpDown
        }
        recyclerView = view.findViewById(R.id.rv_chooser)
        recyclerView.layoutManager = LinearLayoutManager(context)

        view.findViewById<Button>(R.id.button_cancel).setOnClickListener {
            dismiss()
        }
    }

    fun addItems(
        bottomChooserModels: ArrayList<BottomAccountChooserModel>,
        onItemChooser: OnItemChooser
    ): BottomAccountChooserDialog {
        val bottomChooserAdapter = BottomAccountChooserAdapter()
        bottomChooserAdapter.addItems(
            bottomChooserModels,
            object : BottomAccountChooserAdapter.OnItemClick {
                override fun onDelete(position: Int, data: BottomAccountChooserModel) {
                    onItemChooser.onDelete(position, data)
                    dismiss()
                }

                override fun onClick(position: Int, data: BottomAccountChooserModel) {
                    onItemChooser.onChose(position, data)
                    dismiss()
                }
            })
        recyclerView.adapter = bottomChooserAdapter

        return this
    }


    interface OnItemChooser {
        fun onChose(position: Int, data: BottomAccountChooserModel)
        fun onDelete(position: Int, data: BottomAccountChooserModel)
    }


    class BottomAccountChooserModel(var id: String, var name: String, var deleted: Boolean)

}
