package com.example.defenseapplication.home

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.bumptech.glide.Glide
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.PersonalFragmentBinding
import com.example.defenseapplication.liveVideo.LinkedUserActivity
import com.example.defenseapplication.personal.PasswordManagementActivity
import com.example.defenseapplication.personal.PersonalProfileActivity
import com.example.defenseapplication.utils.LanguageUtil
import com.example.defenseapplication.view.FamilySwitchDialog
import com.example.defenseapplication.view.LanguageDialog
import com.gyf.immersionbar.ImmersionBar
//我的碎片
class PersonalFragment : Fragment() {
    // ViewBinding
    private var _binding: PersonalFragmentBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = PersonalFragmentBinding.inflate(inflater, container, false)
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBar(binding.headerLayout)
            .init()
        Glide.with(this)
            .load(R.drawable.img)
            .circleCrop()
            .into(binding.ivAvatar)
        binding.headerLayout.setOnClickListener {
            val intent= Intent(requireContext(), PersonalProfileActivity::class.java)
            startActivity(intent)
        }
        // 更新当前语言显示
        updateCurrentLanguageDisplay()
        // 语言设置点击事件
        binding.itemLanguage.setOnClickListener {
            showLanguageDialog()
        }
        // 切换家庭点击事件
        binding.itemSwitchFamily.setOnClickListener {
            showFamilySwitchDialog()
        }
        //用户管理点击事件跳转关联用户页面
        binding.itemUserManage.setOnClickListener {
            val intent = Intent(requireContext(), LinkedUserActivity::class.java)
            intent.putExtra(LinkedUserActivity.EXTRA_MODE, LinkedUserActivity.MODE_PERSONAL)
            startActivity(intent)
        }
        // 密码管理点击事件
        binding.itemPwdManage.setOnClickListener {
            val intent= Intent(requireContext(), PasswordManagementActivity::class.java)
            startActivity(intent)
        }
        return binding.root
    }


    /**
     * 更新当前语言显示
     */
    private fun updateCurrentLanguageDisplay() {
        val currentLang = LanguageUtil.getCurrentLanguage(requireContext())
        binding.tvCurrentLanguage.text = LanguageUtil.getLanguageDisplayName(currentLang)
    }

    /**
     * 显示语言选择对话框
     */
    private fun showLanguageDialog() {
        val dialog = LanguageDialog(requireContext()) { language ->
            LanguageUtil.switchLanguage(requireContext(), language)
            // 刷新当前页面以应用新语言
            requireActivity().recreate()
        }
        dialog.show()
    }

    /**
     * 显示家庭切换对话框
     */
    private fun showFamilySwitchDialog() {
        val dialog = FamilySwitchDialog(requireContext()) { familyName ->
            binding.tvCurrentFamily.text = familyName
        }
        dialog.show()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null // 避免内存泄漏
    }
}