package com.ads.cal.picturetranslate.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.OptIn;
import androidx.camera.core.ExperimentalGetImage;
import androidx.fragment.app.Fragment;

import com.ads.cal.picturetranslate.R;
import com.ads.cal.picturetranslate.activity.AboutActivity;
import com.ads.cal.picturetranslate.activity.PrivacyPolicyActivity;

import java.util.Objects;

@ExperimentalGetImage
public class SettingFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initView(inflater);
    }

    private View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_setting, null, false);
        RelativeLayout share_layout = view.findViewById(R.id.share_layout);
        RelativeLayout email_layout = view.findViewById(R.id.email_layout);
        RelativeLayout policy_layout = view.findViewById(R.id.policy_layout);
        RelativeLayout about_layout = view.findViewById(R.id.about_layout);
        share_layout.setOnClickListener(v -> {
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            Activity activity = getActivity();
            if (activity != null) {
                String packageName = activity.getPackageName();
                shareIntent.putExtra(Intent.EXTRA_TEXT, "The best Text Extraction app ! \n https://play.google.com/store/apps/details?id=" + packageName); // 设置要分享的内容
            }
            startActivity(Intent.createChooser(shareIntent, getString(R.string.app_share_to)));
        });
        email_layout.setOnClickListener(v -> {
            try {
                // 创建一个Intent，指定Action为发送邮件
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                // 设置邮件的类型为“text/plain”
                emailIntent.setType("text/plain");
                // 添加收件人（可选）
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"zl@gmail.com"});
                // 设置要使用的Gmail应用程序包名
                emailIntent.setPackage("com.google.android.gm");
                startActivity(emailIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        policy_layout.setOnClickListener(v -> PrivacyPolicyActivity.startPrivacyPolicyActivity(getContext()));
        about_layout.setOnClickListener(v -> AboutActivity.startAboutActivity(getContext()));
        return view;
    }
}
