package com.khmer9.yuki.base

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.Guideline
import androidx.fragment.app.Fragment
import com.khmer9.yuki.R
import com.khmer9.yuki.app.App
import com.khmer9.yuki.remote.LottoSet
import com.mazenrashed.printooth.Printooth
import com.mazenrashed.printooth.data.printable.Printable
import com.mazenrashed.printooth.data.printable.TextPrintable
import com.mazenrashed.printooth.data.printer.DefaultPrinter
import com.mazenrashed.printooth.utilities.Printing
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*

abstract class BaseFragment(@LayoutRes private val layoutRes: Int) : Fragment() {
    private var printing: Printing? = null


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

        if (Printooth.hasPairedPrinter()) printing = Printooth.printer()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(layoutRes, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        /*val guideLeft = view.findViewById<Guideline>(R.id.left_guideline)
        val guideRight = view.findViewById<Guideline>(R.id.right_guideline)
        val leftParams = guideLeft.layoutParams as ConstraintLayout.LayoutParams
        leftParams.guidePercent = App.guidelineLeft
        val rightParams = guideRight.layoutParams as ConstraintLayout.LayoutParams
        rightParams.guidePercent = App.guidelineRight
        guideRight.layoutParams = rightParams*/
    }

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

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

    @SuppressLint("SimpleDateFormat")
    open fun formatShowDate(showDate: Date): String {
        val formatter = SimpleDateFormat(("dd/MM/yyyy HH:mm:ss"))
        return formatter.format(showDate)
    }

    fun findLottoSet(lottoSet: List<LottoSet>, type_of_betting: String): LottoSet {
        return lottoSet.find { type_of_betting.equals(it.typeOfBetting, ignoreCase = true) }!!
    }

    fun animateTextView(initialValue: Int, finalValue: Int, textview: TextView): ValueAnimator {
        val valueAnimator = ValueAnimator.ofInt(initialValue, finalValue)
        valueAnimator.duration = 50
        valueAnimator.addUpdateListener { animator ->
            textview.text = animator.animatedValue.toString()
        }
        valueAnimator.start()
        return valueAnimator


    }

    fun printSomePrintable(userName: String, ticketID: String, gameID: String, betDate: String, betType: String, betCast: String, winLose: String) {
        if (Printooth.hasPairedPrinter() && printing == null) printing = Printooth.printer()
        if (printing == null) {
            Toast.makeText(requireContext(), "No printing paired", Toast.LENGTH_SHORT).show()
        } else {
            val printables = getSomePrintables(userName, ticketID, gameID, betDate, betType, betCast, winLose)
            printing?.print(printables)
        }
    }

    private fun getSomePrintables(userName: String, ticketID: String, gameID: String, betDate: String, betType: String, betCast: String, winLose: String) = ArrayList<Printable>().apply {

        add(
            TextPrintable.Builder()
                .setText(userName)
                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC850)
                .build()
        )

        add(
            TextPrintable.Builder()
                .setText("----------------")
                .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC1252)
                .build()
        )
        add(
            TextPrintable.Builder()
                .setText("Ticket ID:   $ticketID")
                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC850)
                .build()
        )
        add(
            TextPrintable.Builder()
                .setText("Game ID  :   $gameID")
                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC850)

                .build()
        )
        add(
            TextPrintable.Builder()
                .setText("Date     :   $betDate")
                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC850)

                .build()
        )
        add(
            TextPrintable.Builder()
                .setText("Bet Type :   $betType")
                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC850)

                .build()
        )
        add(
            TextPrintable.Builder()
                .setText("Bet Point:   $betCast")
                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC850)
                .build()
        )
        add(
            TextPrintable.Builder()
                .setText("Win/Lose :   $winLose")
                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC850)
                .build()
        )
        add(
            TextPrintable.Builder()
                .setText("----------------")
                .setLineSpacing(DefaultPrinter.LINE_SPACING_30)
                .build()
        )
        add(
            TextPrintable.Builder()
                .setText("www.mohasombath.com")
                .setEmphasizedMode(DefaultPrinter.EMPHASIZED_MODE_NORMAL)
                .setAlignment(DefaultPrinter.ALIGNMENT_CENTER)
                .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
                .setCharacterCode(DefaultPrinter.CHARCODE_PC437)
                .setNewLinesAfter(1)
                .build()
        )

    }
}