package com.ytpm.hydtw.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ImmersionBar
import com.ytpm.hydtw.activity.AboutUsActivity
import com.ytpm.hydtw.activity.FeedbackActivity
import com.ytpm.hydtw.adapter.MineAdapter
import com.ytpm.hydtw.dao.Record
import com.ytpm.hydtw.databinding.FragmentMineBinding
import com.ytpm.hydtw.utils.DateTimeConverter
import com.ytpm.hydtw.utils.GlideUtils
import com.ytpm.hydtw.utils.UserInfoSpUtils

class MineFragment : Fragment() {

    private lateinit var binding: FragmentMineBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentMineBinding.inflate(inflater)
        ImmersionBar.with(this)
            .transparentStatusBar()
            .statusBarDarkFont(true)
            .init()
        initView()
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    private fun initView() {

        val headImg = UserInfoSpUtils().getHeadImg()
        context?.let { GlideUtils().circelImages(it, headImg, binding.head) }

        val nickname = UserInfoSpUtils().getNickName()
        binding.nickname.text = nickname

        val uid = UserInfoSpUtils().getUserId()
        binding.userid.text = uid

        val registerTime = UserInfoSpUtils().getRegistryTime()
        val ddd = DateTimeConverter.convertToBeijingTime(registerTime, "yyyy/MM/dd HH:mm:ss")
        binding.registerTime.text = "注册时间:${ddd}"

        val loginTime = UserInfoSpUtils().getLoginTime()
        val ccc = DateTimeConverter.convertToBeijingTime(loginTime, "yyyy/MM/dd HH:mm:ss")
        binding.loginTime.text = "登录时间:${ccc}"

        val jrdd = UserInfoSpUtils().getTodayAnswerCount()
        val lsdd = UserInfoSpUtils().getHistoryAnswerCount()

        binding.jrddTv.text = "今日答题: ${jrdd}题"
        binding.lsddTv.text = "历史答题: ${lsdd}题"

        binding.fragmentMineRv.adapter = MineAdapter(initData())
        binding.fragmentMineAbout.setOnClickListener {
            val intent = Intent(it.context, AboutUsActivity::class.java)
            startActivity(intent)
        }
        binding.fragmentMineFeedback.setOnClickListener {
            val intent = Intent(it.context, FeedbackActivity::class.java)
            startActivity(intent)
        }
    }

    private fun initData(): MutableList<Record> {
        val dfdfd = UserInfoSpUtils().getAnswerRecordList()
        return dfdfd.toMutableList()
    }
}