package com.prank.master.ui;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.prank.master.adapter.likeAdapter;
import com.prank.master.databinding.FragmentRecordBinding;
import com.prank.master.db.Bean;
import com.prank.master.db.ObManager;
import com.prank.master.utils.ConstValues;

import java.util.List;


public class RecordFragment extends Fragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private FragmentRecordBinding binding;
    private likeAdapter adapter;
    private ActivityResultLauncher<String>  requestRecordAudioLauncher;

    public RecordFragment() {
        // Required empty public constructor
    }

    public static RecordFragment newInstance() {
        RecordFragment fragment = new RecordFragment();
        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        requestRecordAudioLauncher = registerForActivityResult(
                new ActivityResultContracts.RequestPermission(),
                isGranted -> {
                    if (isGranted) {
                        intentRecordAudio();
                    } else {
                        Toast.makeText(requireContext(), "Recording permission denied", Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentRecordBinding.inflate(getLayoutInflater());


        binding.recyclerImport.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        List<Bean> all = ObManager.getAllCustomization();
        updateUi(all);
        adapter = new likeAdapter(requireContext(), all);
        binding.recyclerImport.setAdapter(adapter);
        binding.recordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAndRequestAudioPermission();
            }
        });

        return binding.getRoot();
    }


    public void refresh() {
        if (adapter != null) {
            List<Bean> all = ObManager.getAllCustomization();
            updateUi(all);
            adapter.updateData(all);
        }

    }

    private void updateUi(List<Bean> all) {
        if (all.size() == 0) {
            binding.noImportHint.setVisibility(View.VISIBLE);
            binding.recyclerImport.setVisibility(View.GONE);
        } else {
            binding.noImportHint.setVisibility(View.GONE);
            binding.recyclerImport.setVisibility(View.VISIBLE);
        }
    }

    private void checkAndRequestAudioPermission() {
        if (ContextCompat.checkSelfPermission(requireContext(), ConstValues.permission_record_audio)
                != PackageManager.PERMISSION_GRANTED) {
            requestRecordAudioLauncher.launch(ConstValues.permission_record_audio);
        } else {
            intentRecordAudio();
        }
    }

    private void intentRecordAudio(){
        Intent intent = new Intent(requireContext(), RecordAudioActivity.class);
        startActivity(intent);
    }
}