package com.sambath.kunkhmer.screen.account

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.app.App
import com.sambath.kunkhmer.remote.User
import com.sambath.kunkhmer.screen.login.LoginActivity
import com.sambath.kunkhmer.util.Const
import com.sambath.kunkhmer.util.ModelPreferencesManager
import com.sambath.kunkhmer.util.PrefHelper
import kotlinx.android.synthetic.main.fragment_account.view.country_tv
import kotlinx.android.synthetic.main.fragment_account.view.email_tv
import kotlinx.android.synthetic.main.fragment_account.view.logout_btn
import kotlinx.android.synthetic.main.fragment_account.view.member_tv
import kotlinx.android.synthetic.main.fragment_account.view.username_tv
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.TimeZone

class AccountFragment : Fragment() {
    private var _root: View? = null
    private val binding get() = _root!!
    private lateinit var user: User
    private lateinit var accountViewModel: AccountViewModel
    lateinit var sharePref: PrefHelper

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

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        _root = inflater.inflate(R.layout.fragment_account, container, false)
        user = ModelPreferencesManager.get<User>(Const.USER_KEY)!!
        sharePref = App.injectPrefHelper()

        binding.username_tv.text = user.userName
        accountViewModel = AccountViewModel(App.injectApiService(), App.injectPrefHelper())
        accountViewModel.getCurrentUser()
        accountViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            render(it)
        })

        binding.logout_btn.setOnClickListener{
            // Clear user-related data or preferences, such as the user token.
            sharePref.logout()
            // Redirect the user to the login screen or perform any other necessary actions.
            // For example, you can start a new activity or fragment for the login screen:
            val intent = Intent(context, LoginActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            startActivity(intent)

        }

        return _root
    }

    private fun render(it: AccountViewModel.AccountViewState) {
        if(it.isLoginSuccess){
            if(it.currentUser != null) {
                val data = it.currentUser

                binding.member_tv.text = parseAndFormatDate(data.createdAt)
                binding.email_tv.text = data.email_or_phone
                binding.country_tv.text = "Cambodia"

            }
        }
    }

    private fun parseAndFormatDate(dateString: String): String {
        // Create a SimpleDateFormat for parsing the date string
        val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
        parser.timeZone = TimeZone.getTimeZone("UTC")

        // Parse the date
        val date = parser.parse(dateString)

        // Create a SimpleDateFormat for formatting the date
        val formatter = SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault())
        formatter.timeZone = TimeZone.getDefault() // Use local timezone

        // Format the date
        return formatter.format(date)
    }
}