package com.sambath.kunkhmer.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 androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import com.sambath.kunkhmer.remote.LottoSet
import com.sambath.kunkhmer.remote.User
import com.sambath.kunkhmer.util.Const
import com.sambath.kunkhmer.util.ModelPreferencesManager
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*

abstract class BaseFragment(
    @LayoutRes private val layoutRes: Int
) : Fragment() {
    private lateinit var user: User
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        user = ModelPreferencesManager.get<User>(Const.USER_KEY)!!
    }

//    fun currencyType(): String {
//        when (user.currencyType) {
//            1 -> {//dollar
//                return "$"
//            }
//
//            2 -> {//riel
//                return "៛"
//            }
//
//            3 -> {//baht
//                return "฿"
//            }
//        }
//        return ""
//    }

    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)
    }

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

    fun currencyFormatKh(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 == it.typeOfBetting }!!
    }

    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


    }

    private fun getLastFourChars(str: String, n: Int): String {
        var lastChars = str
        if (lastChars.length > n) {
            lastChars = lastChars.substring(lastChars.length - n, lastChars.length)
        }
        return lastChars
    }
}