package com.offline.music.player.ui.fragmnt.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.offline.music.player.MusicApplication;
import com.offline.music.player.helper.JsonUtils;
import com.offline.music.player.javabean.A_data.AudioItem;
import com.offline.music.player.javabean.A_data.Category;

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

public class A_VMHome extends ViewModel {

    private MutableLiveData<List<AudioItem>> soundOfInstrumentList;
    private MutableLiveData<List<AudioItem>> whiteNoiseList;
    private MutableLiveData<List<AudioItem>> voiceOfNatureList;

    public LiveData<List<AudioItem>> getSoundOfInstrumentList() {
        if (soundOfInstrumentList == null) {
            soundOfInstrumentList = new MutableLiveData<>();
            loadAudioData();
        }
        return soundOfInstrumentList;
    }

    public LiveData<List<AudioItem>> getWhiteNoiseList() {
        if (whiteNoiseList == null) {
            whiteNoiseList = new MutableLiveData<>();
            loadAudioData();
        }
        return whiteNoiseList;
    }

    public LiveData<List<AudioItem>> getVoiceOfNatureList() {
        if (voiceOfNatureList == null) {
            voiceOfNatureList = new MutableLiveData<>();
            loadAudioData();
        }
        return voiceOfNatureList;
    }

    private void loadAudioData() {
        new Thread(() -> {
            try {
                String jsonString = JsonUtils.readJsonFromAssets(MusicApplication.myApplication, "resources.json");
                if (jsonString != null && !jsonString.isEmpty()) {
                    List<Category> categories = JsonUtils.parseJson(jsonString);

                    List<AudioItem> soundOfInstrumentItems = new ArrayList<>();
                    List<AudioItem> whiteNoiseItems = new ArrayList<>();
                    List<AudioItem> voiceOfNatureItems = new ArrayList<>();

                    for (Category category : categories) {
                        String categoryName = category.getName();
                        List<AudioItem> audioItems = category.getAudios();

                        switch (categoryName) {
                            case "Sound of instrument":
                                soundOfInstrumentItems.addAll(audioItems);
                                break;
                            case "White noise":
                                whiteNoiseItems.addAll(audioItems);
                                break;
                            case "Voice of Nature":
                                voiceOfNatureItems.addAll(audioItems);
                                break;
                        }
                    }

                    // 更新对应的 LiveData
                    soundOfInstrumentList.postValue(soundOfInstrumentItems);
                    whiteNoiseList.postValue(whiteNoiseItems);
                    voiceOfNatureList.postValue(voiceOfNatureItems);
                } else {
                    soundOfInstrumentList.postValue(new ArrayList<>());
                    whiteNoiseList.postValue(new ArrayList<>());
                    voiceOfNatureList.postValue(new ArrayList<>());
                }
            } catch (Exception e) {
                e.printStackTrace();
                soundOfInstrumentList.postValue(new ArrayList<>());
                whiteNoiseList.postValue(new ArrayList<>());
                voiceOfNatureList.postValue(new ArrayList<>());
            }
        }).start();
    }
}
