package com.prank.simulator.sounds.actfrag;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.bumptech.glide.Glide;
import com.prank.simulator.sounds.PrankApp;
import com.prank.simulator.sounds.R;
import com.prank.simulator.sounds.data.Category;
import com.prank.simulator.sounds.data.ChildData;
import com.prank.simulator.sounds.databinding.FragHomeBinding;
import com.prank.simulator.sounds.helper.ItemHelper;
import com.prank.simulator.sounds.recycler.HomeAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class HomeFragment extends Fragment {


    private FragHomeBinding binding;
    private MediaPlayer mediaPlayer;
    private ChildData childData;
    private List<ChildData> list;
    private boolean first = true;

    public HomeFragment() {
    }

    @SuppressWarnings("unused")
    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragHomeBinding.inflate(inflater);
        ItemHelper itemHelper = new ItemHelper(10, 10, 5);
        binding.recyclerView.addItemDecoration(itemHelper);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 3));
        binding.recyclerView.setAdapter(new HomeAdapter(requireContext(), PrankApp.bean));
        initAudioData();
        binding.linearPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    binding.btnPlay.setSelected(false);
                    return;
                }
                if (first) {
                    binding.btnPlay.setSelected(true);
                    first = false;
                    mediaPlayer.start();
                } else {
                    playAudio(true);
                }


            }
        });

        return binding.getRoot();
    }

    private void initAudioData() {
        list = new ArrayList<>();
        for (Category category : PrankApp.bean) {
            list.addAll(category.getChildDataList());
        }
        initAudio();
    }

    private void initAudio() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                binding.btnPlay.setSelected(false);
            }
        });
        playAudio(false);
    }


    private void playAudio(boolean play) {
        Collections.shuffle(list);
        childData = list.get(0);
        Glide.with(this)
                .load(childData.getCovert())
                .placeholder(R.drawable.tab_home0)
                .into(binding.audioIm);
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(childData.getAudioUrl());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (play) {
                        binding.btnPlay.setSelected(true);
                        mediaPlayer.start();
                    }
                }
            });

        } catch (IOException ioException) {


        }
    }
}