package com.acitelight.aether.viewModel import android.content.Context import android.net.Uri import android.widget.Toast import androidx.annotation.OptIn import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.media3.common.MediaItem import androidx.media3.common.PlaybackException import androidx.media3.common.Player import androidx.media3.common.Player.STATE_READY import androidx.media3.common.text.Cue import androidx.media3.common.util.UnstableApi import androidx.media3.datasource.okhttp.OkHttpDataSource import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.source.DefaultMediaSourceFactory import coil3.ImageLoader import coil3.network.okhttp.OkHttpNetworkFetcherFactory import com.acitelight.aether.model.Video import com.acitelight.aether.model.VideoQueryIndex import com.acitelight.aether.model.VideoRecord import com.acitelight.aether.model.VideoRecordDatabase import com.acitelight.aether.service.ApiClient.createOkHttp import com.acitelight.aether.service.MediaManager import com.acitelight.aether.service.RecentManager import com.acitelight.aether.view.formatTime import com.acitelight.aether.view.hexToString import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import okhttp3.Request import java.io.File import javax.inject.Inject import androidx.core.net.toUri import androidx.media3.common.Tracks import androidx.media3.datasource.DefaultDataSource import androidx.media3.exoplayer.trackselection.DefaultTrackSelector import com.acitelight.aether.Global import com.acitelight.aether.model.KeyImage import com.acitelight.aether.service.VideoLibrary import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.first @HiltViewModel class VideoPlayerViewModel @Inject constructor( @ApplicationContext private val context: Context, val mediaManager: MediaManager, val recentManager: RecentManager, val videoLibrary: VideoLibrary, ) : ViewModel() { var tabIndex by mutableIntStateOf(0) var isPlaying by mutableStateOf(true) var playProcess by mutableFloatStateOf(0.0f) var planeVisibility by mutableStateOf(true) var isLongPressing by mutableStateOf(false) // -1 : Not dragging // 0 : Seek // 1 : Volume // 2 : Brightness var draggingPurpose by mutableIntStateOf(-1) var locked by mutableStateOf(false) private var _init: Boolean = false var startPlaying by mutableStateOf(false) var renderedFirst = false var videos: List