package com.khmer9.yuki.adapter

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.recyclerview.widget.RecyclerView
import com.khmer9.yuki.R
import com.khmer9.yuki.app.App
import com.khmer9.yuki.remote.ReportResultRequest
import java.text.SimpleDateFormat
import java.util.*

class ResultStatementHeaderAdapter : RecyclerView.Adapter<ResultStatementHeaderAdapter.ViewHolder>() {
    private val myCalendar: Calendar = Calendar.getInstance()
    private val shareViewModel = App.injectShareViewModel()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // inflates the card_view_design view
        // that is used to hold list item
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.layout_result_statement_header, parent, false)

        val edtStartDate = view.findViewById<EditText>(R.id.edt_start_date)
        val edtEndDate = view.findViewById<EditText>(R.id.edt_end_date)
        val btnSearch = view.findViewById<Button>(R.id.btn_filter)

        //------------------------------- filter search

        val dateStart = OnDateSetListener { view, year, month, day ->
            myCalendar.set(Calendar.YEAR, year)
            myCalendar.set(Calendar.MONTH, month)
            myCalendar.set(Calendar.DAY_OF_MONTH, day)
            updateLabel(edtStartDate)
        }

        val dateEnd = OnDateSetListener { view, year, month, day ->
            myCalendar.set(Calendar.YEAR, year)
            myCalendar.set(Calendar.MONTH, month)
            myCalendar.set(Calendar.DAY_OF_MONTH, day)
            updateLabel(edtEndDate)
        }

        edtStartDate.setOnClickListener(View.OnClickListener {
            DatePickerDialog(
                view.context,
                dateStart,
                myCalendar[Calendar.YEAR],
                myCalendar[Calendar.MONTH],
                myCalendar[Calendar.DAY_OF_MONTH]
            ).show()
        })

        edtEndDate.setOnClickListener(View.OnClickListener {
            DatePickerDialog(
                view.context,
                dateEnd,
                myCalendar[Calendar.YEAR],
                myCalendar[Calendar.MONTH],
                myCalendar[Calendar.DAY_OF_MONTH]
            ).show()
        })

        btnSearch.setOnClickListener(View.OnClickListener {
            val startDate = edtStartDate.text.toString()
            val endDate = edtEndDate.text.toString()
            val request = ReportResultRequest(startDate, endDate, 20, 1)

            shareViewModel.remoteReportResultStatement(request)
        })

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    }

    override fun getItemCount(): Int {
        return 1
    }

    class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
    }

    private fun updateLabel(edt: EditText) {
        val myFormat = "yyyy-MM-dd"
        val dateFormat = SimpleDateFormat(myFormat, Locale.US)
        edt.setText(dateFormat.format(myCalendar.time))
    }
}