package com.sound.prankparty.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.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.sound.MainApplication;
import com.sound.prankparty.Activity.PrivacyActivity;
import com.sound.prankparty.Adapter.SoundRecyclerViewAdapter;
import com.sound.prankparty.JSON.Category;
import com.sound.prankparty.R;
import com.sound.prankparty.Utils.ItemDecoration;
import com.sound.prankparty.Utils.JsonParser;
import com.sound.prankparty.Utils.LoadJSON;
import com.sound.prankparty.databinding.FragmentSoundBinding;

import java.util.List;

public class SoundFragment extends Fragment {

    private FragmentSoundBinding binding;
    private PackageInfo packageInfo;
    private String format;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // 使用 ViewBinding 初始化视图
        binding = FragmentSoundBinding.inflate(inflater, container, false);
        View view = binding.getRoot();

        // 设置 RecyclerView 的布局管理器和适配器
        binding.soundRecyclerview.setLayoutManager(new GridLayoutManager(getContext(), 2));

        // 从 assets 文件夹中读取 JSON 文件
        String jsonString = LoadJSON.loadJSONFromAsset("prank.json");
        // 解析读取的 String
        List<Category> categories = JsonParser.parseJson(jsonString);

        binding.soundRecyclerview.setAdapter(new SoundRecyclerViewAdapter(getContext(), categories));

        // 为 RecyclerView 添加自定义的间距装饰
        ItemDecoration itemDecoration = new ItemDecoration(16, 19, 10);
        binding.soundRecyclerview.addItemDecoration(itemDecoration);

        // 设置返回按钮的点击事件
        binding.fragmentSoundSetting.setOnClickListener(v -> showCustomBottomSheetDialog());

        return view;
    }

    private void showCustomBottomSheetDialog() {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(requireContext());
        View dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.setting_dialog, null);
        // 禁止点击其他页面取消
        bottomSheetDialog.setCanceledOnTouchOutside(false);

        TextView version = dialogView.findViewById(R.id.version_number);

        //更新版本号
        try {
            packageInfo = MainApplication.getContext().getPackageManager().getPackageInfo(MainApplication.getContext().getPackageName(), 0);
            String versionName = packageInfo.versionName;
            format = String.format(getString(R.string.gp), packageInfo.packageName);
            version.setText(versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            version.setText("1.0.0");
            format = String.format(getString(R.string.gp), "com.sound.prankparty");
        }


        dialogView.findViewById(R.id.privacy).setOnClickListener(v -> {
            // 处理选项 1
            Intent intent = new Intent(requireActivity(), PrivacyActivity.class);
            startActivity(intent);
            // 销毁 BottomSheetDialog 对象
            bottomSheetDialog.dismiss();
        });

        dialogView.findViewById(R.id.share).setOnClickListener(v -> {

            shareString(format);
            // 处理选项 2
            bottomSheetDialog.dismiss();
        });

        dialogView.findViewById(R.id.version).setOnClickListener(v -> {

        });

        dialogView.findViewById(R.id.cancel_button).setOnClickListener(v -> {

            // 处理取消按钮
            bottomSheetDialog.dismiss();
        });

        bottomSheetDialog.setContentView(dialogView);
        bottomSheetDialog.show();
    }

    /**
     * 分享纯文本
     *
     * @param content 内容
     */
    public void shareString(String content) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, content);//分享的文本内容
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, "Share to"));
    }

}
