package com.prank.master.ui;

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.master.MyApp;
import com.prank.master.R;
import com.prank.master.adapter.HomeAdapter;
import com.prank.master.databinding.FragmentHomeBinding;
import com.prank.master.db.Bean;
import com.prank.master.db.Category;
import com.prank.master.utils.MyDecoration;

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


public class HomeFragment extends Fragment {


    private FragmentHomeBinding binding;
    private MediaPlayer mediaPlayer;
    private Bean bean;
    private List<Bean> 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 = FragmentHomeBinding.inflate(inflater);
        MyDecoration myDecoration = new MyDecoration(10, 10, 5);
        binding.recyclerView.addItemDecoration(myDecoration);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 3));
        binding.recyclerView.setAdapter(new HomeAdapter(requireContext(), MyApp.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 : MyApp.bean) {
            list.addAll(category.getBeanList());
        }
        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);
        bean = list.get(0);
        Glide.with(this)
                .load(bean.getCovert())
                .placeholder(R.drawable.tab_home_no)
                .into(binding.audioIm);
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(bean.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) {


        }
    }
}