package com.idle.wall.idlefragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.idle.wall.databinding.FragmentIdleSettingBinding;
import com.idle.wall.idleactivity.IdleRateDialog;

public class FragmentIdleSetting extends Fragment {

    private FragmentIdleSettingBinding vb;

    public static FragmentIdleSetting newInstance() {
        return new FragmentIdleSetting();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        vb = FragmentIdleSettingBinding.inflate(inflater, container, false);
        initEdgeToEdge();
        initVersionName();
        initClickListeners();
        return vb.getRoot();
    }

    private void initVersionName() {
        try {
            PackageInfo packageInfo = requireContext().getPackageManager()
                    .getPackageInfo(requireContext().getPackageName(), 0);
            vb.tvVersion.setText("v" + packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            vb.tvVersion.setText("v1.0.0");
        }
    }

    private void initEdgeToEdge() {
        if (getActivity() != null) {
            ViewCompat.setOnApplyWindowInsetsListener(vb.main, (v, insets) -> {
                Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
                return insets;
            });
        }
    }

    private void initClickListeners() {
        // 仅保留原有评分按钮点击事件
        vb.tvRateUs.setOnClickListener(v -> {
            if (getActivity() != null) {
                new IdleRateDialog(getActivity()).show();
            }
        });
        vb.llRateUs.setOnClickListener(v -> {
            if (getActivity() != null) {
                new IdleRateDialog(getActivity()).show();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        vb = null;
    }
}