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 android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.khmer9.yuki.R
import com.khmer9.yuki.app.App
import com.khmer9.yuki.remote.LoginData
import com.khmer9.yuki.remote.ReportWithdrawRequest
import com.khmer9.yuki.remote.User
import com.khmer9.yuki.util.Const
import com.khmer9.yuki.util.ModelPreferencesManager
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*

class WithdrawStatementHeaderAdapter(val totalAmount: String?) : RecyclerView.Adapter<WithdrawStatementHeaderAdapter.ViewHolder>() {
    private val myCalendar: Calendar = Calendar.getInstance()
    private val shareViewModel = App.injectShareViewModel()
    private lateinit var user: User

    fun currencyFormat(amount: Double): String? {
        val formatter = DecimalFormat("#,###.##")
        return formatter.format(amount)
    }

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

        val tvTurnOver = view.findViewById<TextView>(R.id.tv_total_amount)
        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)

        ModelPreferencesManager.get<LoginData>(Const.USER_KEY)?.let {
            user = it.user!!
        }

        //------------------------------- 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 = ReportWithdrawRequest(startDate, endDate, 20, 1)

            shareViewModel.remoteReportWithdrawStatement(request)
        })

        //------------------------------- set value
        if (totalAmount != null) {
            //tvTurnOver.text = "${currencyFormat(totalAmount.toDouble())}"

            when (user.currencyType) {
                1 -> {//dollar
                    tvTurnOver.text = "${currencyFormat(totalAmount.toDouble())} $"
                }
                2 -> {//riel
                    tvTurnOver.text = "${currencyFormat(totalAmount.toDouble())} ៛"
                }
                3 -> {//baht
                    tvTurnOver.text = "${currencyFormat(totalAmount.toDouble())} ฿"
                }
            }
        }

        return WithdrawStatementHeaderAdapter.ViewHolder(view)
    }

    override fun onBindViewHolder(holder: WithdrawStatementHeaderAdapter.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))
    }
}