package com.offline.music.player.helper;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;

public class SettingUtil {
    public static final String PRIVACY_POLICY_URL = "https://himelody.mystrikingly.com/privacy";
    public static final String TERMS_OF_SERVICE_URL = "https://himelody.mystrikingly.com/terms";

    // 获取当前应用的版本号
    public static String getCurrentVersion(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName; // 返回版本名称
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null; // 返回null表示获取版本号失败
        }
    }

    // 发送反馈
    public static void sendFeedback(Context context, String email, String subject) {
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("message/rfc822");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        try {
            context.startActivity(Intent.createChooser(emailIntent, "Send Feedback"));
        } catch (ActivityNotFoundException ex) {
            Toast.makeText(context, "There is no app that supports sending emails", Toast.LENGTH_LONG).show();
        }
    }

    // 分享应用
    public static void shareApp(Context context) {
        String appPackageName = context.getPackageName();
        String appName = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
        String appPlayStoreLink = "https://play.google.com/store/apps/details?id=" + appPackageName;

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Check out this app: " + appName);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "Download " + appName + " from Google Play: " + appPlayStoreLink);
        context.startActivity(Intent.createChooser(shareIntent, "Share " + appName + " via"));
    }

    // 打开隐私政策
    public static void openPrivacyPolicy(Context context) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(PRIVACY_POLICY_URL));
        context.startActivity(intent);
    }

    // 打开服务条款
    public static void openTermsOfService(Context context) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(TERMS_OF_SERVICE_URL));
        context.startActivity(intent);
    }

    // 打开Google Play
    public static void openGooglePlay(Context context, String packageName) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
}
