package com.snap.wall.fragment;

import android.content.Intent;
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 android.widget.Toast;

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.snap.wall.activity.RateDialog;
import com.snap.wall.databinding.FragmentSettingBinding;

public class FragmentSetting extends Fragment {

    private FragmentSettingBinding vb;

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

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        vb = FragmentSettingBinding.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 RateDialog(getActivity()).show();
            }
        });
        vb.llRateUs.setOnClickListener(v -> {
            if (getActivity() != null) {
                new RateDialog(getActivity()).show();
            }
        });

    }


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