package com.sound.prankparty.FirstFragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.sound.prankparty.JSON.Category;
import com.sound.prankparty.R;
import com.sound.prankparty.SecondFragment.NewRecordActivity;
import com.sound.prankparty.Utils.ItemDecoration;
import com.sound.prankparty.Utils.JsonParser;
import com.sound.prankparty.Utils.LoadJSON;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.List;


public class SoundFragment extends Fragment {

    ImageView btnBack;

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

        View view = inflater.inflate(R.layout.fragment_sound, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.sound_recyclerview);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

        btnBack = view.findViewById(R.id.fragment_sound_setting);

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

        recyclerView.setAdapter(new SoundRecyclerViewAdapter(getContext(), categories));

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

        btnBack.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);

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

        dialogView.findViewById(R.id.share).setOnClickListener(v -> {
            // 处理选项 2
            bottomSheetDialog.dismiss();
        });

        dialogView.findViewById(R.id.version).setOnClickListener(v -> {
            // 处理取消按钮
            bottomSheetDialog.dismiss();
        });

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


}