package com.doverfuelingsolutions.issp.view.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.api.dto.ResultPayment
import com.doverfuelingsolutions.issp.databinding.FragmentPayResultBinding
import com.doverfuelingsolutions.issp.driver.lib.PrintManager
import com.doverfuelingsolutions.issp.utils.DFSToastUtil
import com.doverfuelingsolutions.issp.view.MainActivity
import com.wayne.www.waynelib.fdc.message.DeviceClass
import kotlinx.android.synthetic.main.widget_fragment_toolbar.*
import kotlinx.coroutines.launch

/**
 * 支付结果页面（目前只有成功才会显示该页面）
 */
class FragmentPayResult private constructor(
    private val dc: DeviceClass,
    private val posTrxBean: ResultPayment.PosTrxBean
) : FragmentBasic(), View.OnClickListener {

    companion object {
        fun build(dc: DeviceClass, posTrxBean: ResultPayment.PosTrxBean) = FragmentPayResult(dc, posTrxBean)
    }

    private lateinit var binding: FragmentPayResultBinding
    private val viewModel = PayResultViewModel()

    private val printManager = PrintManager()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycle.addObserver(printManager)
        (activity as MainActivity).blockBackPress(true)
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pay_result, container, false)
        binding.lifecycleOwner = this
        binding.viewModel = viewModel
        viewModel.physicalNozzleId.value = dc.nozzleNo
        viewModel.amount.value = posTrxBean.NetAmount
        binding.handler = this
        setToolBar(binding.fragmentToolbar)
        return binding.root
    }

    override fun onDestroy() {
        super.onDestroy()
        (activity as MainActivity).blockBackPress(false)
    }

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

        print()
    }

    override fun onClick(v: View?) {
        when (v) {
            binding.buttonFinish -> {
                home()
            }
            binding.buttonRePrint -> {
                print()
            }
        }
    }

    private fun print() {
        lifecycleScope.launch {
            val result = printManager.print(dc, posTrxBean)
            if (result.success) {
                viewModel.isReprint.value = false
            } else {
                viewModel.isReprint.value = true
                DFSToastUtil.fail(R.string.fail_print_again)
            }
        }
    }

    class PayResultViewModel : ViewModel() {
        val physicalNozzleId = MutableLiveData(0)
        val amount = MutableLiveData(0.0)
        val isReprint = MutableLiveData(false)
    }
}