// VideoEoxPlayer：支援影片播放 + 圖片輪播優化版本
package com.unibuy.smartdevice.ui.tools;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;

import androidx.annotation.OptIn;
import androidx.media3.common.MediaItem;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.Player;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DefaultDataSource;
import androidx.media3.exoplayer.DefaultLoadControl;
import androidx.media3.exoplayer.DefaultRenderersFactory;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import androidx.media3.extractor.DefaultExtractorsFactory;
import androidx.media3.ui.PlayerView;

import com.bumptech.glide.Glide;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.database.SettingsDao;
import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.exception.LogsFileNullException;
import com.unibuy.smartdevice.exception.LogsIOException;
import com.unibuy.smartdevice.exception.LogsMalformedURLException;
import com.unibuy.smartdevice.structure.SettingStructure;
import com.unibuy.smartdevice.tools.HttpConnect;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VideoEoxPlayer {
    private final Logs logs;
    private final Context context;
    private ExoPlayer exoPlayer;
    private PlayerView currentPlayerView;
    private ImageView imageView;
    private Handler imageHandler;
    private Runnable imageRunnable;
    private List<String> imageUrls = new ArrayList<>();
    private int currentImageIndex = 0;

    private static int exoPlayerTotal;
    // 新增成員變數
    private VideoImageMixedPlayer mixedPlayer;

    public static int getExoPlayerTotal() {
        return exoPlayerTotal;
    }

    public static void setExoPlayerTotal(int exoPlayerTotal) {
        VideoEoxPlayer.exoPlayerTotal = exoPlayerTotal;
    }

    public VideoEoxPlayer(Context context) {
        this.context = context;
        this.logs = new Logs(getClass());
    }

       /**
     * 👉 新增：依據 flag 播放資料庫裡的廣告
     */
    public void playMediaByFlag(String targetFlag, PlayerView videoView, ImageView imageView) throws LogsFileNullException, LogsEmptyException {
        this.imageView = imageView;
        this.currentPlayerView = videoView;

        setExoPlayerTotal(MyApp.getInstance().getFontendAdUrlString().size());

        SettingsDao settingsDao = new SettingsDao(context);
        List<SettingStructure> dataList = settingsDao.getWhere(VideoEoxPlayer.class.getSimpleName());
        List<VideoImageMixedPlayer.AdItem> targetAds = new ArrayList<>();

        for (SettingStructure data : dataList) {
            String url = data.getData(0);
            String flag = data.getData(1);
            int duration = 10;
            try {
                if (data.getDataCount() > 2) {
                    duration = Integer.parseInt(data.getData(2));
                }
            } catch (Exception ignored) {}

            if (flag != null && flag.equals(targetFlag)) {
                targetAds.add(new VideoImageMixedPlayer.AdItem(url, duration));
            }
        }
        settingsDao.close();
        logs.info("Ads [Flag: " + targetFlag + "] 過濾後數量: " + targetAds.size());

        mixedPlayer = new VideoImageMixedPlayer(context, targetAds);
        mixedPlayer.start(videoView, imageView);

    }

    public void stopVideo() {
        if (mixedPlayer != null) {
            mixedPlayer.stop();
        }
    }
}
