package com.lh.wallpaper2.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.RelativeLayout;

import com.lh.wallpaper2.PrivacyActivity;
import com.lh.wallpaper2.R;

public class SettingFragment extends Fragment {
    private RelativeLayout about;
    private RelativeLayout feedBack;
    private RelativeLayout privacy;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_setting, container, false);
        about = view.findViewById(R.id.about);
        feedBack=view.findViewById(R.id.feedback);
        privacy=view.findViewById(R.id.Privacy);

        privacy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(requireContext(), PrivacyActivity.class);
                startActivity(intent);
            }
        });
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        feedBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                @SuppressLint("IntentWithNullActionLaunch")
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?id="+requireContext().getPackageName()));
                startActivity(intent);
            }
        });
        return view;
    }
    public void onBackPressed(){
        //声明对象
        final AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        builder.setTitle("About");

        builder.setMessage("The current version is "+getVersion());
        //添加确定按钮
        builder.setPositiveButton("CONFIRM", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,int which) {
                dialog.dismiss();   //关闭对话框
            }
        });
        dialog = builder.create();
        dialog.show();
    }
    public String getVersion(){
        try {
            PackageInfo packageInfo =requireContext().getPackageManager().getPackageInfo(requireContext().getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
