package com.prank.tool.frag;

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.prank.tool.MyApp;
import com.prank.tool.jbean.Info;
import com.prank.tool.jbean.MyBean;
import com.prank.tool.databinding.FragmentHomeBinding;
import com.prank.tool.utils.Space;
import com.prank.tool.adapter.AdapterHome;

import java.util.ArrayList;
import java.util.List;


public class FragmentMain extends Fragment {


    private FragmentHomeBinding binding;

    private List<Info> list;
    private boolean first = true;

    public FragmentMain() {
    }

    @SuppressWarnings("unused")
    public static FragmentMain newInstance() {
        FragmentMain fragment = new FragmentMain();
        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);

        Space itemHelper = new Space(15, 25, 10);
        binding.recyclerView.addItemDecoration(itemHelper);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        binding.recyclerView.setAdapter(new AdapterHome(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 (MyBean category : MyApp.bean) {
            list.addAll(category.getInfoList());
        }
        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())
//                .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) {
//
//
//        }
//    }
}