package com.pan_american.android.ui.group_hotel.hotel_predetermine

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.base.BaseFragment
import com.pan_american.android.base.ListAdapter
import com.pan_american.android.data.model.common.entity.Selector
import com.pan_american.android.data.model.group_hotel.hotel_predetermine.entity.HotelReservationsContentDetailsView
import com.pan_american.android.databinding.FragmentHotelPredeterminePaymentBinding
import com.pan_american.android.databinding.LayoutPaymentApplicationBinding
import com.pan_american.android.util.MoneyInputFilter
import org.greenrobot.eventbus.EventBus
import java.text.DecimalFormat

class HotelPredeterminePaymentFragment : BaseFragment<FragmentHotelPredeterminePaymentBinding>() {

    private var fromList = false

    private var canBeEdit = false

    private var days = 0

    private var bankCardCurrencyId = 0

    private var bankCardRate = 0.0

    private var bankCardCurrencyStr = ""

    private var singleRoomCount = 0

    private var singleRoomUnitPrice = 0.0

    private var singleRoomPrice = 0.0

    private var singleRoomPricePre = 0.0

    private var doubleRoomCount = 0

    private var doubleRoomUnitPrice = 0.0

    private var doubleRoomPrice = 0.0

    private var doubleRoomPricePre = 0.0

    private var suiteRoomCount = 0

    private var suiteRoomUnitPrice = 0.0

    private var suiteRoomPrice = 0.0

    private var suiteRoomPricePre = 0.0

    private var otherRoomCount = 0

    private var otherRoomUnitPrice = 0.0

    private var otherRoomPrice = 0.0

    private var otherRoomPricePre = 0.0

    private var breakfastPrice = 0.0

    private var localTaxPayment = 0.0

    private var cityTaxPayment = 0.0

    private var creditPayment = 0.0

    private var sumPayment = 0.0

    private var isBreakfastCurrency = true

    private var isLocalTaxCurrency = true

    private var isCityTaxCurrency = true

    private var breakfastUnitId = 0

    private var breakfastRate = 0.0

    private var localTaxUnitId = 0

    private var localTaxRate = 0.0

    private var cityTaxUnitId = 0

    private var cityTaxRate = 0.0

    private var sumUnitCurrencyId = 0

    private var sumUnitCurrencyStr = ""

    private val regex = Regex("^-?\\d+(\\.\\d{1,2})?\$")

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        bundle: Bundle?
    ) = FragmentHotelPredeterminePaymentBinding.inflate(layoutInflater, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        requireArguments().apply {
            fromList = getBoolean("fromList")
            canBeEdit = getBoolean("canBeEdit")
            days = getInt("days")
        }

        initViews()
        initEvents()
    }

    override fun initViews() {

        binding.hotelPredetermine.singleRoomPrice.filters = arrayOf(MoneyInputFilter())
        binding.hotelPredetermine.doubleRoomPrice.filters = arrayOf(MoneyInputFilter())
        binding.hotelPredetermine.suiteRoomPrice.filters = arrayOf(MoneyInputFilter())
        binding.hotelPredetermine.otherRoomPrice.filters = arrayOf(MoneyInputFilter())
        binding.hotelBreakfast.filters = arrayOf(MoneyInputFilter())
        binding.localTax.filters = arrayOf(MoneyInputFilter())
        binding.cityTax.filters = arrayOf(MoneyInputFilter())

        if (canBeEdit) {
            binding.commit.setText(resources.getString(R.string.update))
        } else {

            binding.hotelPredetermine.apply {
                hotelPaymentCurrency.isEnabled = false
                realitySingleRoom.isEnabled = false
                singleRoomPrice.isEnabled = false
                realityDoubleRoom.isEnabled = false
                doubleRoomPrice.isEnabled = false
                realitySuiteRoom.isEnabled = false
                suiteRoomPrice.isEnabled = false
                realityOtherRoom.isEnabled = false
                otherRoomPrice.isEnabled = false
                hotelYes.isEnabled = false
                hotelNo.isEnabled = false
            }

            binding.cityTax.isEnabled = false
            binding.cityTaxUnit.isEnabled = false
            binding.cityTaxYes.isEnabled = false
            binding.cityTaxNo.isEnabled = false

            binding.localTax.isEnabled = false
            binding.localTaxUnit.isEnabled = false
            binding.localTaxYes.isEnabled = false
            binding.localTaxNo.isEnabled = false

            binding.hotelBreakfast.isEnabled = false
            binding.hotelBreakfastUnit.isEnabled = false
            binding.breakfastYes.isEnabled = false
            binding.breakfastNo.isEnabled = false

            binding.paymentApplyNotes.isEnabled(false)

            binding.commit.setText(resources.getString(R.string.back))
        }

        if (!fromList or (OASystem.hotelPredetermineRequest.contents.size == 0)) {
            for (i in 1 .. 4) {
                val hotelContent = HotelReservationsContentDetailsView()

                hotelContent.priceType = i

                OASystem.hotelPredetermineRequest.contents.add(hotelContent)
            }
        }

        binding.hotelPredetermine.apply {
            realitySingleRoom.setText(OASystem.hotelPredetermineRequest.singleRoomCount.toString())
            singleRoomCount = OASystem.hotelPredetermineRequest.singleRoomCount
            singleRoomPrice.setText(OASystem.hotelPredetermineRequest.singleRoomPrice.toString())
            singleRoomUnitPrice = OASystem.hotelPredetermineRequest.singleRoomPrice

            realityDoubleRoom.setText(OASystem.hotelPredetermineRequest.doubleRoomCount.toString())
            doubleRoomCount = OASystem.hotelPredetermineRequest.doubleRoomCount
            doubleRoomPrice.setText(OASystem.hotelPredetermineRequest.doubleRoomPrice.toString())
            doubleRoomUnitPrice = OASystem.hotelPredetermineRequest.doubleRoomPrice

            realitySuiteRoom.setText(OASystem.hotelPredetermineRequest.suiteRoomCount.toString())
            suiteRoomCount = OASystem.hotelPredetermineRequest.suiteRoomCount
            suiteRoomPrice.setText(OASystem.hotelPredetermineRequest.suiteRoomPrice.toString())
            suiteRoomUnitPrice = OASystem.hotelPredetermineRequest.suiteRoomPrice

            realityOtherRoom.setText(OASystem.hotelPredetermineRequest.otherRoomCount.toString())
            otherRoomCount = OASystem.hotelPredetermineRequest.otherRoomCount
            otherRoomPrice.setText(OASystem.hotelPredetermineRequest.otherRoomPrice.toString())
            otherRoomUnitPrice = OASystem.hotelPredetermineRequest.otherRoomPrice
        }

        singleRoomPrice = singleRoomUnitPrice * singleRoomCount * days
        doubleRoomPrice = doubleRoomUnitPrice * doubleRoomCount * days
        suiteRoomPrice = suiteRoomUnitPrice * suiteRoomCount * days
        otherRoomPrice = otherRoomUnitPrice * otherRoomCount * days

        for (item in OASystem.hotelPredetermineRequest.contents) {
            when (item.priceType) {
                1 -> {

                    creditPayment = item.price
                    bankCardCurrencyId = item.currency
                    bankCardRate = item.rate

                    for (currency in OASystem.hotelPredeterminePaymentCurrency) {
                        if (currency.currencyId == bankCardCurrencyId) {
                            binding.hotelPredetermine.hotelPaymentCurrency.text = currency.currencyCode

                            binding.hotelPredetermine.singleRoomUnit.text = currency.currencyCode
                            binding.hotelPredetermine.doubleRoomUnit.text = currency.currencyCode
                            binding.hotelPredetermine.suiteRoomUnit.text = currency.currencyCode
                            binding.hotelPredetermine.otherRoomUnit.text = currency.currencyCode
                            binding.hotelPredetermine.hotelPredetermineCurrency.text = String.format(resources.getString(R.string.with_space_format), currency.currencyCode, currency.rate.toString())


                            bankCardCurrencyStr = currency.currencyCode
                        }
                    }

                    when(item.isOppay) {
                        1 -> {
                            binding.hotelPredetermine.hotelYes.isChecked = true
                        }

                        0 -> {
                            binding.hotelPredetermine.hotelNo.isChecked = true
                        }
                    }

                    binding.hotelPredetermine.creditCardPayment.text = String.format(resources.getString(R.string.price_and_currency_format), creditPayment, bankCardCurrencyStr)

                    initPaymentApplicationViews(binding.hotelPaymentApplication, item)
                }

                2 -> {
                    binding.hotelBreakfast.setText(item.price.toString())

                    breakfastPrice = item.price
                    breakfastUnitId = item.currency
                    breakfastRate = item.rate

                    for (currency in OASystem.hotelPredeterminePaymentCurrency) {
                        if (currency.currencyId == breakfastUnitId) {

                            binding.hotelBreakfastUnit.text = currency.currencyCode
                            binding.breakfastCurrency.text = String.format(resources.getString(R.string.with_space_format), currency.currencyCode, currency.rate.toString())

                        }
                    }

                    when(item.isOppay) {
                        1 -> {
                            binding.breakfastYes.isChecked = true
                        }

                        0 -> {
                            binding.breakfastNo.isChecked = true
                        }
                    }

                    initPaymentApplicationViews(binding.breakfastPaymentApplication, item)
                }

                3 -> {
                    binding.localTax.setText(item.price.toString())

                    localTaxPayment = item.price
                    localTaxUnitId = item.currency
                    localTaxRate = item.rate

                    for (currency in OASystem.hotelPredeterminePaymentCurrency) {
                        if (currency.currencyId == localTaxUnitId) {
                            binding.localTaxUnit.text = currency.currencyCode
                            binding.localTaxCurrency.text = String.format(resources.getString(R.string.with_space_format), currency.currencyCode, currency.rate.toString())

                        }
                    }

                    when(item.isOppay) {
                        1 -> {
                            binding.localTaxYes.isChecked = true
                        }

                        0 -> {
                            binding.localTaxNo.isChecked = true
                        }
                    }

                    initPaymentApplicationViews(binding.localTaxPaymentApplication, item)
                }

                4 -> {
                    binding.cityTax.setText(item.price.toString())

                    cityTaxPayment = item.price
                    cityTaxUnitId = item.currency
                    cityTaxRate = item.rate

                    for (currency in OASystem.hotelPredeterminePaymentCurrency) {
                        if (currency.currencyId == cityTaxUnitId) {

                            binding.cityTaxUnit.text = currency.currencyCode
                            binding.cityTaxCurrency.text = String.format(resources.getString(R.string.with_space_format), currency.currencyCode, currency.rate.toString())

                        }
                    }

                    when(item.isOppay) {
                        1 -> {
                            binding.cityTaxYes.isChecked = true
                        }

                        0 -> {
                            binding.cityTaxNo.isChecked = true
                        }
                    }

                    initPaymentApplicationViews(binding.cityTaxPaymentApplication, item)
                }
            }
        }

        binding.paymentApplyNotes.setText(OASystem.hotelPredetermineRequest.ccpRemark)

        for (item in OASystem.hotelPredeterminePaymentCurrency) {
            if (OASystem.hotelPredetermineRequest.cardPriceCurrency == item.currencyId) {
                binding.paymentSum.text = String.format(resources.getString(R.string.price_and_currency_format), OASystem.hotelPredetermineRequest.cardPrice, item.currencyCode)
            }
        }
    }

    override fun initEvents() {

        binding.hotelPredetermine.hotelPaymentCurrency.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(OASystem.hotelPredeterminePaymentCurrency)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = String.format(resources.getString(R.string.currency_name_code_format), data.currencyName, data.currencyCode)

                            if (bankCardCurrencyId == data.currencyId) {
                                setTextColor(
                                    ResourcesCompat.getColor(
                                        resources, R.color.title_background_color, null
                                    )
                                )
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick = object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                    override fun onItemClick(position: Int) {
                        OASystem.hotelPredeterminePaymentCurrency[position].apply {
                            binding.hotelPredetermine.hotelPaymentCurrency.text = currencyCode
                            binding.hotelPredetermine.singleRoomUnit.text = currencyCode
                            binding.hotelPredetermine.doubleRoomUnit.text = currencyCode
                            binding.hotelPredetermine.suiteRoomUnit.text = currencyCode
                            binding.hotelPredetermine.otherRoomUnit.text = currencyCode
                            binding.hotelPredetermine.hotelPredetermineCurrency.text = String.format(resources.getString(R.string.with_space_format), currencyCode, rate.toString())

                            bankCardCurrencyId = currencyId
                            bankCardCurrencyStr = currencyCode
                            bankCardRate = rate

                            popupWindow.dismiss()

                            calculateRoomPayment(5)
                        }
                    }
                }
            }
        }

        binding.hotelPredetermine.realitySingleRoom.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {

                if (binding.hotelPredetermine.realitySingleRoom.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.single_room_count_input_error))
                    return
                } else {
                    singleRoomCount = binding.hotelPredetermine.realitySingleRoom.text.toString().toInt()

                    calculateRoomPayment(1)
                }
            }
        })

        binding.hotelPredetermine.singleRoomPrice.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.hotelPredetermine.singleRoomPrice.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.single_room_price_input_error))
                    return
                }

                if (binding.hotelPredetermine.singleRoomPrice.text.endsWith('.')) {
                    return
                }

                if (binding.hotelPredetermine.singleRoomPrice.text.matches(regex)) {

                    singleRoomUnitPrice = binding.hotelPredetermine.singleRoomPrice.text.toString().toDouble()

                    calculateRoomPayment(1)
                }
            }
        })

        binding.hotelPredetermine.realityDoubleRoom.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.hotelPredetermine.realityDoubleRoom.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.double_room_count_input_error))
                    return
                }

                doubleRoomCount = binding.hotelPredetermine.realityDoubleRoom.text.toString().toInt()

                calculateRoomPayment(2)
            }
        })

        binding.hotelPredetermine.doubleRoomPrice.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.hotelPredetermine.doubleRoomPrice.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.double_room_price_input_error))
                    return
                }

                if (binding.hotelPredetermine.doubleRoomPrice.text.endsWith('.')) {
                    return
                }

                if (binding.hotelPredetermine.doubleRoomPrice.text.matches(regex)) {

                    doubleRoomUnitPrice = binding.hotelPredetermine.doubleRoomPrice.text.toString().toDouble()

                    calculateRoomPayment(2)
                }
            }
        })

        binding.hotelPredetermine.realitySuiteRoom.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.hotelPredetermine.realitySuiteRoom.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.suite_room_count_input_error))
                    return
                }

                suiteRoomCount = binding.hotelPredetermine.realitySuiteRoom.text.toString().toInt()

                calculateRoomPayment(3)
            }
        })

        binding.hotelPredetermine.suiteRoomPrice.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.hotelPredetermine.suiteRoomPrice.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.suite_room_price_input_error))
                    return
                }

                if (binding.hotelPredetermine.suiteRoomPrice.text.endsWith('.')) {
                    return
                }

                if (binding.hotelPredetermine.suiteRoomPrice.text.matches(regex)) {

                    suiteRoomUnitPrice = binding.hotelPredetermine.suiteRoomPrice.text.toString().toDouble()

                    calculateRoomPayment(3)
                }
            }
        })

        binding.hotelPredetermine.realityOtherRoom.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.hotelPredetermine.realityOtherRoom.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.other_room_count_input_error))
                    return
                }

                otherRoomCount = binding.hotelPredetermine.realityOtherRoom.text.toString().toInt()

                calculateRoomPayment(4)
            }
        })

        binding.hotelPredetermine.otherRoomPrice.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.hotelPredetermine.otherRoomPrice.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.other_room_price_input_error))
                    return
                }

                if (binding.hotelPredetermine.otherRoomPrice.text.endsWith('.')) {
                    return
                }

                if (binding.hotelPredetermine.otherRoomPrice.text.matches(regex)) {

                    otherRoomUnitPrice = binding.hotelPredetermine.otherRoomPrice.text.toString().toDouble()

                    calculateRoomPayment(4)
                }
            }
        })

        binding.hotelPredetermine.hotelOpPay.setOnCheckedChangeListener { radioGroup, i ->

            if (radioGroup.id == binding.hotelPredetermine.hotelOpPay.id) {
                for (item in OASystem.hotelPredetermineRequest.contents) {
                    if (item.priceType == 1) {
                        when(i) {
                            binding.hotelPredetermine.hotelYes.id -> {
                                item.isOppay = 1
                                item.payDId = 73
                            }

                            binding.hotelPredetermine.hotelNo.id -> {
                                item.isOppay = 0
                                item.payDId = 72
                            }
                        }

                        initPaymentApplicationViews(binding.hotelPaymentApplication, item)
                    }
                }
            }
        }

        binding.cityTax.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.cityTax.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.city_tax_input_error))
                    return
                }

                if (binding.cityTax.text.endsWith('.')) {
                    return
                }

                if (binding.cityTax.text.matches(regex)) {

                    cityTaxPayment = binding.cityTax.text.toString().toDouble()

                    calculateTotalPayment()
                }
            }
        })

        binding.cityTaxUnit.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(OASystem.hotelPredeterminePaymentCurrency)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = String.format(resources.getString(R.string.currency_name_code_format), data.currencyName, data.currencyCode)

                            if (data.currencyId == cityTaxUnitId) {
                                setTextColor(
                                    ResourcesCompat.getColor(
                                        resources, R.color.title_background_color, null
                                    )
                                )
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick = object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                    override fun onItemClick(position: Int) {
                        OASystem.hotelPredeterminePaymentCurrency[position].apply {
                            binding.cityTaxUnit.text = currencyCode
                            binding.cityTaxCurrency.text = String.format(resources.getString(R.string.with_space_format), currencyCode, rate.toString())

                            cityTaxUnitId = currencyId
                            cityTaxRate = rate

                            calculateTotalPayment()

                            popupWindow.dismiss()
                        }
                    }
                }
            }
        }

        binding.cityTaxOpPay.setOnCheckedChangeListener { radioGroup, i ->
            if (radioGroup.id == binding.cityTaxOpPay.id) {
                for (item in OASystem.hotelPredetermineRequest.contents) {
                    if (item.priceType == 4) {
                        when(i) {
                            binding.cityTaxYes.id -> {
                                item.isOppay = 1
                                item.payDId = 73
                            }

                            binding.cityTaxNo.id -> {
                                item.isOppay = 0
                                item.payDId = 72
                            }
                        }

                        initPaymentApplicationViews(binding.cityTaxPaymentApplication, item)
                    }
                }
            }
        }

        binding.localTax.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.localTax.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.local_tax_input_error))
                    return
                }

                if (binding.localTax.text.endsWith('.')) {
                    return
                }

                if (binding.localTax.text.matches(regex)) {

                    localTaxPayment = binding.localTax.text.toString().toDouble()

                    calculateTotalPayment()
                }
            }
        })

        binding.localTaxUnit.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(OASystem.hotelPredeterminePaymentCurrency)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = String.format(resources.getString(R.string.currency_name_code_format), data.currencyName, data.currencyCode)

                            if (data.currencyId == localTaxUnitId) {
                                setTextColor(
                                    ResourcesCompat.getColor(
                                        resources, R.color.title_background_color, null
                                    )
                                )
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick = object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                    override fun onItemClick(position: Int) {
                        OASystem.hotelPredeterminePaymentCurrency[position].apply {
                            binding.localTaxUnit.text = currencyCode
                            binding.localTaxCurrency.text = String.format(resources.getString(R.string.with_space_format), currencyCode, rate.toString())


                            localTaxUnitId = currencyId
                            localTaxRate = rate

                            calculateTotalPayment()

                            popupWindow.dismiss()
                        }
                    }
                }
            }
        }

        binding.localTaxOpPay.setOnCheckedChangeListener { radioGroup, i ->
            if (radioGroup.id == binding.localTaxOpPay.id) {
                for (item in OASystem.hotelPredetermineRequest.contents) {
                    if (item.priceType == 3) {
                        when(i) {
                            binding.localTaxYes.id -> {
                                item.isOppay = 1
                                item.payDId = 73
                            }

                            binding.localTaxNo.id -> {
                                item.isOppay = 0
                                item.payDId = 72
                            }
                        }

                        initPaymentApplicationViews(binding.localTaxPaymentApplication, item)
                    }
                }
            }
        }

        binding.hotelBreakfast.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                if (binding.hotelBreakfast.text.isNullOrBlank()) {
                    showMessage(resources.getString(R.string.breakfast_input_error))
                    return
                }

                if (binding.hotelBreakfast.text.endsWith('.')) {
                    return
                }

                if (binding.hotelBreakfast.text.matches(regex)) {

                    breakfastPrice = binding.hotelBreakfast.text.toString().toDouble()

                    calculateTotalPayment()
                }
            }
        })

        binding.hotelBreakfastUnit.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(OASystem.hotelPredeterminePaymentCurrency)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = String.format(resources.getString(R.string.currency_name_code_format), data.currencyName, data.currencyCode)

                            if (data.currencyId == breakfastUnitId) {
                                setTextColor(
                                    ResourcesCompat.getColor(
                                        resources, R.color.title_background_color, null
                                    )
                                )
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick = object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                    override fun onItemClick(position: Int) {
                        OASystem.hotelPredeterminePaymentCurrency[position].apply {
                            binding.hotelBreakfastUnit.text = currencyCode
                            binding.breakfastCurrency.text = String.format(resources.getString(R.string.with_space_format), currencyCode, rate.toString())


                            breakfastUnitId = currencyId
                            breakfastRate = rate

                            calculateTotalPayment()

                            popupWindow.dismiss()
                        }
                    }
                }
            }
        }

        binding.breakfastOpPay.setOnCheckedChangeListener { radioGroup, i ->
            if (radioGroup.id == binding.breakfastOpPay.id) {
                for (item in OASystem.hotelPredetermineRequest.contents) {
                    if (item.priceType == 2) {
                        when(i) {
                            binding.breakfastYes.id -> {
                                item.isOppay = 1
                                item.payDId = 73
                            }

                            binding.breakfastNo.id -> {
                                item.isOppay = 0
                                item.payDId = 72
                            }
                        }

                        initPaymentApplicationViews(binding.breakfastPaymentApplication, item)
                    }
                }
            }
        }

        for (item in OASystem.hotelPredetermineRequest.contents) {
            when (item.priceType) {
                1 -> {
                    initPaymentApplicationEvents(binding.hotelPaymentApplication, item)
                }
                2 -> {
                    initPaymentApplicationEvents(binding.breakfastPaymentApplication, item)
                }
                3 -> {
                    initPaymentApplicationEvents(binding.localTaxPaymentApplication, item)
                }
                4 -> {
                    initPaymentApplicationEvents(binding.cityTaxPaymentApplication, item)
                }
            }
        }

        binding.commit.setOnClickListener {

            if (!canBeEdit) {
                requireActivity().finish()
                return@setOnClickListener
            }

            OASystem.hotelPredetermineRequest.apply {
                singleRoomCount = this@HotelPredeterminePaymentFragment.singleRoomCount
                singleRoomPrice = singleRoomUnitPrice
                doubleRoomCount = this@HotelPredeterminePaymentFragment.doubleRoomCount
                doubleRoomPrice = doubleRoomUnitPrice
                suiteRoomCount = this@HotelPredeterminePaymentFragment.suiteRoomCount
                suiteRoomPrice = suiteRoomUnitPrice
                otherRoomCount = this@HotelPredeterminePaymentFragment.otherRoomCount
                otherRoomPrice = otherRoomUnitPrice
                cardPrice = sumPayment
                cardPriceCurrency = sumUnitCurrencyId

                for (item in contents) {
                    when(item.priceType) {
                        1 -> {
                            item.price = creditPayment
                            item.currency = bankCardCurrencyId
                            item.rate = bankCardRate

//                            when(binding.hotelPredetermine.hotelOpPay.checkedRadioButtonId) {
//                                binding.hotelPredetermine.hotelYes.id -> {
//                                    item.isOppay = 1
//                                }
//
//                                binding.hotelPredetermine.hotelNo.id -> {
//                                    item.isOppay = 0
//                                }
//                            }
                        }

                        2 -> {
                            item.price = breakfastPrice
                            item.currency = breakfastUnitId
                            item.rate = breakfastRate

//                            when(binding.breakfastOpPay.checkedRadioButtonId) {
//                                binding.breakfastYes.id -> {
//                                    item.isOppay = 1
//                                }
//
//                                binding.breakfastNo.id -> {
//                                    item.isOppay = 0
//                                }
//                            }
                        }

                        3 -> {
                            item.price = localTaxPayment
                            item.currency = localTaxUnitId
                            item.rate = localTaxRate

//                            when(binding.localTaxOpPay.checkedRadioButtonId) {
//                                binding.localTaxYes.id -> {
//                                    item.isOppay = 1
//                                }
//                                binding.localTaxNo.id -> {
//                                    item.isOppay = 0
//                                }
//                            }
                        }

                        4 -> {
                            item.price = cityTaxPayment
                            item.currency = cityTaxUnitId
                            item.rate = cityTaxRate

//                            when(binding.cityTaxOpPay.checkedRadioButtonId) {
//                                binding.cityTaxYes.id -> {
//                                    item.isOppay = 1
//                                }
//                                binding.cityTaxNo.id -> {
//                                    item.isOppay = 0
//                                }
//                            }
                        }
                    }
                }

                ccpRemark = binding.paymentApplyNotes.getText()
            }

            EventBus.getDefault().post(OASystem.hotelPredetermineRequest)
        }
    }

    private fun calculateRoomPayment(type: Int) {

        when (type) {
            1 -> {
                singleRoomPricePre = singleRoomPrice

                singleRoomPrice = singleRoomUnitPrice * singleRoomCount * days

                creditPayment -= singleRoomPricePre - singleRoomPrice
            }

            2 -> {
                doubleRoomPricePre = doubleRoomPrice

                doubleRoomPrice = doubleRoomUnitPrice * doubleRoomCount * days

                creditPayment -= doubleRoomPricePre - doubleRoomPrice
            }

            3 -> {
                suiteRoomPricePre = suiteRoomPrice

                suiteRoomPrice = suiteRoomUnitPrice * suiteRoomCount * days

                creditPayment -= suiteRoomPricePre - suiteRoomPrice
            }

            4 -> {
                otherRoomPricePre = otherRoomPrice

                otherRoomPrice = otherRoomUnitPrice * otherRoomCount * days

                creditPayment -= otherRoomPricePre - otherRoomPrice
            }

            5 -> {
                singleRoomPricePre = singleRoomPrice

                singleRoomPrice = singleRoomUnitPrice * singleRoomCount * days

                creditPayment -= singleRoomPricePre - singleRoomPrice

                doubleRoomPricePre = doubleRoomPrice

                doubleRoomPrice = doubleRoomUnitPrice * doubleRoomCount * days

                creditPayment -= doubleRoomPricePre - doubleRoomPrice

                suiteRoomPricePre = suiteRoomPrice

                suiteRoomPrice = suiteRoomUnitPrice * suiteRoomCount * days

                creditPayment -= suiteRoomPricePre - suiteRoomPrice

                otherRoomPricePre = otherRoomPrice

                otherRoomPrice = otherRoomUnitPrice * otherRoomCount * days

                creditPayment -= otherRoomPricePre - otherRoomPrice
            }
        }

        val decimalFormat = DecimalFormat("#.##")
        creditPayment = decimalFormat.format(creditPayment).toDouble()

        binding.hotelPredetermine.creditCardPayment.text = String.format(resources.getString(R.string.price_and_currency_format), creditPayment, bankCardCurrencyStr)

        calculateTotalPayment()
    }

    private fun calculateTotalPayment() {

        isBreakfastCurrency = breakfastUnitId == bankCardCurrencyId

        isLocalTaxCurrency = localTaxUnitId == bankCardCurrencyId

        isCityTaxCurrency = cityTaxUnitId == bankCardCurrencyId

        if (isBreakfastCurrency && isLocalTaxCurrency && isCityTaxCurrency) {
            sumPayment = creditPayment + breakfastPrice + localTaxPayment + cityTaxPayment

            sumUnitCurrencyId = bankCardCurrencyId

            sumUnitCurrencyStr = bankCardCurrencyStr

        } else {
            sumPayment =
                creditPayment * bankCardRate + breakfastPrice * breakfastRate + localTaxPayment * localTaxRate + cityTaxPayment * cityTaxRate

            run {
                for (item in OASystem.hotelPredeterminePaymentCurrency) {
                    if (item.currencyCode == "CNY") {
                        sumUnitCurrencyId = item.currencyId
                        sumUnitCurrencyStr = item.currencyCode
                        return@run
                    }
                }
            }
        }

        val decimalFormat = DecimalFormat("#.##")
        sumPayment = decimalFormat.format(sumPayment).toDouble()

        binding.paymentSum.text = String.format(resources.getString(R.string.price_and_currency_format), sumPayment, sumUnitCurrencyStr)
    }

    private fun initPaymentApplicationViews(binding: LayoutPaymentApplicationBinding, content: HotelReservationsContentDetailsView) {

        for (item in OASystem.hotelPredeterminePaymentWay) {
            if (item.id == content.payDId) {
                binding.paymentWay.text = item.name
            }
        }

        binding.consumeWay.setText(content.consumptionPatterns)

        if (content.consumptionDate.isNotBlank()) {
            binding.consumeDate.text = content.consumptionDate
        }

        when (content.payDId) {
            //刷卡
            72 -> {
                binding.swipe.visibility = View.VISIBLE
                binding.transfer.visibility = View.GONE
                binding.remit.visibility = View.GONE

                for (item in OASystem.hotelPredetermineBankCard) {
                    if (item.id == content.ctdId) {
                        binding.cardType.text = item.name
                        binding.bankCardNumber.text = item.remark
                        binding.bankCardOwner.text = item.cardholder
                    }
                }
            }

            //转账
            73 -> {
                binding.swipe.visibility = View.GONE
                binding.transfer.visibility = View.VISIBLE
                binding.remit.visibility = View.GONE

                binding.companyBankNo.setText(content.companyBankNo)

                binding.transferOtherBankName.setText(content.otherBankName)

                binding.transferOtherSideNo.setText(content.otherSideNo)

                binding.transferOtherSideName.setText(content.otherSideName)
            }

            //汇款
            83 -> {
                binding.swipe.visibility = View.GONE
                binding.transfer.visibility = View.GONE
                binding.remit.visibility = View.VISIBLE

                binding.otherBankName.setText(content.otherBankName)

                binding.otherSideNo.setText(content.otherSideNo)

                binding.otherSideName.setText(content.otherSideName)
            }

            else -> {
                binding.swipe.visibility = View.GONE
                binding.transfer.visibility = View.GONE
                binding.remit.visibility = View.GONE
            }
        }

        binding.payee.setText(content.payee)

        binding.paymentMark.text =
            OASystem.paymentMark[content.orbitalPrivateTransfer].name

        if (!canBeEdit) {
            binding.paymentWay.isEnabled = false
            binding.consumeWay.isEnabled = false
            binding.consumeDate.isEnabled = false
            binding.cardType.isEnabled = false
            binding.companyBankNo.isEnabled = false
            binding.transferOtherBankName.isEnabled = false
            binding.transferOtherSideNo.isEnabled = false
            binding.transferOtherSideName.isEnabled = false
            binding.otherBankName.isEnabled = false
            binding.otherSideNo.isEnabled = false
            binding.otherSideName.isEnabled = false
            binding.payee.isEnabled = false
        }

    }

    private fun initPaymentApplicationEvents(binding: LayoutPaymentApplicationBinding, content: HotelReservationsContentDetailsView) {
        binding.paymentWay.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(OASystem.hotelPredeterminePaymentWay)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = data.name

                            if (data.id == content.payDId) {
                                setTextColor(
                                    ResourcesCompat.getColor(
                                        resources, R.color.title_background_color, null
                                    )
                                )
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick = object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                    override fun onItemClick(position: Int) {
                        OASystem.hotelPredeterminePaymentWay[position].apply {
                            content.payDId = id
                            binding.paymentWay.text = name
                        }

                        when (content.payDId) {
                            //刷卡
                            72 -> {
                                binding.swipe.visibility = View.VISIBLE
                                binding.transfer.visibility = View.GONE
                                binding.remit.visibility = View.GONE

                                binding.cardType.text = ""
                                binding.bankCardNumber.text = ""
                                binding.bankCardOwner.text = ""

                                content.ctdId = 0
                            }
                            //转账
                            73 -> {
                                binding.swipe.visibility = View.GONE
                                binding.transfer.visibility = View.VISIBLE
                                binding.remit.visibility = View.GONE
                            }
                            //汇款
                            83 -> {
                                binding.swipe.visibility = View.GONE
                                binding.transfer.visibility = View.GONE
                                binding.remit.visibility = View.VISIBLE
                            }
                            //其它
                            else -> {
                                binding.swipe.visibility = View.GONE
                                binding.transfer.visibility = View.GONE
                                binding.remit.visibility = View.GONE
                            }
                        }

                        popupWindow.dismiss()
                    }
                }
            }
        }

        binding.consumeWay.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(changedText: Editable?) {
                if (changedText.isNullOrBlank()) {
                    content.consumptionPatterns = ""
                } else {
                    content.consumptionPatterns = changedText.toString()
                }
            }
        })

        binding.consumeDate.setOnClickListener {
            showDatePicker(
                resources.getString(R.string.consume_date_select_hint), binding.consumeDate
            ) {
                content.consumptionDate = binding.consumeDate.text.toString()
            }
        }

        binding.cardType.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(OASystem.hotelPredetermineBankCard)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = data.name

                            if (data.id == content.ctdId) {
                                setTextColor(
                                    ResourcesCompat.getColor(
                                        resources, R.color.title_background_color, null
                                    )
                                )
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick =
                    object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                        override fun onItemClick(position: Int) {
                            binding.cardType.text = OASystem.hotelPredetermineBankCard[position].name
                            binding.bankCardNumber.text = OASystem.hotelPredetermineBankCard[position].remark
                            binding.bankCardOwner.text = OASystem.hotelPredetermineBankCard[position].cardholder

                            content.ctdId = OASystem.hotelPredetermineBankCard[position].id
                            content.bankNo = OASystem.hotelPredetermineBankCard[position].remark
                            content.cardholderName = OASystem.hotelPredetermineBankCard[position].cardholder

                            popupWindow.dismiss()
                        }
                    }
            }
        }

        binding.companyBankNo.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(changedText: Editable?) {

                content.companyBankNo = if (changedText.isNullOrBlank()) {
                    ""
                } else {
                    changedText.toString()
                }
            }
        })

        binding.transferOtherBankName.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(changedText: Editable?) {

                content.otherBankName = if (changedText.isNullOrBlank()) {
                    ""
                } else {
                    changedText.toString()
                }
            }
        })

        binding.transferOtherSideNo.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(changedText: Editable?) {

                content.otherSideNo = if (changedText.isNullOrBlank()) {
                    ""
                } else {
                    changedText.toString()
                }
            }
        })

        binding.transferOtherSideName.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(changedText: Editable?) {

                content.otherSideName = if (changedText.isNullOrBlank()) {
                    ""
                } else {
                    changedText.toString()
                }
            }
        })

        binding.otherBankName.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(changedText: Editable?) {
                content.otherBankName = if (changedText.isNullOrBlank()) {
                    ""
                } else {
                    changedText.toString()
                }
            }
        })

        binding.otherSideNo.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(changedText: Editable?) {
                content.otherSideNo = if (changedText.isNullOrBlank()) {
                    ""
                } else {
                    changedText.toString()
                }
            }
        })

        binding.otherSideName.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(changedText: Editable?) {
                content.otherSideName = if (changedText.isNullOrBlank()) {
                    ""
                } else {
                    changedText.toString()
                }
            }
        })

        binding.paymentMark.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<Selector>().apply {
                    setData(OASystem.paymentMark)
                    setLayoutId(R.layout.item_selector)
                    addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = data.name

                            if (data.id == content.orbitalPrivateTransfer) {
                                setTextColor(
                                    ResourcesCompat.getColor(
                                        resources, R.color.title_background_color, null
                                    )
                                )
                            }
                        }
                    }
                }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick =
                    object : ListAdapter.OnRecyclerViewItemClick<Selector> {
                        override fun onItemClick(position: Int) {
                            content.orbitalPrivateTransfer = OASystem.paymentMark[position].id
                            binding.paymentMark.text = OASystem.paymentMark[position].name

                            popupWindow.dismiss()
                        }
                    }
            }
        }
    }
}