package com.wall.photography.picture.fragment;

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.fragment.app.Fragment;

import com.wall.photography.picture.databinding.FragmentSettingsBinding;

public class Settings extends Fragment {

    private FragmentSettingsBinding vb;

    public Settings() {
    }

    public static Settings newInstance() {
        Settings fragment = new Settings();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragmentSettingsBinding.inflate(inflater, container, false);

        // 设置点击事件
        setupClickListeners();

        setVersion();

        return vb.getRoot();
    }

    private void setVersion() {
        try {
            PackageManager pm = requireActivity().getPackageManager();
            PackageInfo pInfo = pm.getPackageInfo(requireActivity().getPackageName(), 0);
            String versionName = pInfo.versionName;
            vb.tvVersionNumber.setText("V " + versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            vb.tvVersionNumber.setText("V 1.0");
        }

    }

    private void setupClickListeners() {
        // Rate us 点击事件 - 显示评星对话框
        if (vb.layoutRateUs != null) {
            vb.layoutRateUs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showRatingDialog();
                }
            });
        }

        // Version - 移除点击监听，不可点击
        if (vb.layoutPrivacy != null) {
            vb.layoutPrivacy.setOnClickListener(null);
            vb.layoutPrivacy.setClickable(false);
            vb.layoutPrivacy.setFocusable(false);
        }
    }

    private void showRatingDialog() {
        RatingDialog ratingDialog = new RatingDialog();
        if (getActivity() != null) {
            ratingDialog.show(getActivity().getSupportFragmentManager(), "RatingDialog");
        }
    }

}
