package com.khmer9.yuki.adapter

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.graphics.Color
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.ReportBettingRequest
import java.text.SimpleDateFormat
import java.util.*


class BettingStatementHeaderAdapter(val turnOver: String?, val totalWinLose: String?) : RecyclerView.Adapter<BettingStatementHeaderAdapter.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_betting_header, parent, false)

        val tvTotalWinLose = view.findViewById<TextView>(R.id.tv_total_win_lose)
        val tvTurnOver = view.findViewById<TextView>(R.id.tv_turn_over)
        val tvTWinLose = view.findViewById<TextView>(R.id.tv_t_win_lose)
        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 = ReportBettingRequest(startDate, endDate, 20, 1)

            shareViewModel.remoteReportBettingStatement(request)
        })

        //----------------------- set value

        if (this.turnOver != null) {
            tvTurnOver.text = turnOver
        } else {
            tvTurnOver.text = "0"
        }

        if (this.totalWinLose != null) {
            tvTotalWinLose.text = totalWinLose
            if (totalWinLose.toDouble() > 0) {
                tvTotalWinLose.setTextColor(Color.parseColor("#303F9F"))
                tvTWinLose.setTextColor(Color.BLACK)
            } else {
                tvTotalWinLose.setTextColor(Color.RED)
                tvTWinLose.setTextColor(Color.BLACK)
            }
        } else {
            tvTotalWinLose.text = "0"
        }

        return ViewHolder(view)
    }

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


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

    override fun getItemCount(): Int {
        return 1;
    }

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