package com.foxx.androidcast.sender;

import android.content.Context;

import androidx.appcompat.app.AppCompatActivity;

import android.widget.Spinner;

import com.foxx.androidcast.BuildConfig;
import com.foxx.androidcast.AppPreferences;
import com.foxx.androidcast.CastSettings;
import com.foxx.androidcast.CastSettingsBinder;
import com.foxx.androidcast.R;

/** Binds {@link R.id#spinner_sender_resolution} per {@link SenderResolutionUiMode}. */
public final class SenderResolutionUi {
    private SenderResolutionUi() {}

    public static SenderResolutionUiMode mode(Context context) {
        if (!BuildConfig.DEBUG) {
            return SenderResolutionUiMode.SEPARATED;
        }
        return AppPreferences.getDevSenderResolutionUiMode(context);
    }

    public static void bindSenderResolution(AppCompatActivity activity, CastSettings settings) {
        Spinner spinner = activity.findViewById(R.id.spinner_sender_resolution);
        if (spinner == null) {
            return;
        }
        switch (mode(activity)) {
            case LEGACY:
                bindLegacy(activity, settings, spinner);
                break;
            case COMBINED:
                bindCombined(activity, settings, spinner);
                break;
            case SEPARATED:
            default:
                bindSeparated(activity, settings, spinner);
                break;
        }
    }

    public static void readSenderResolution(AppCompatActivity activity, CastSettings settings) {
        Spinner spinner = activity.findViewById(R.id.spinner_sender_resolution);
        if (spinner != null) {
            CastSettingsBinder.readEnumSpinner(spinner, settings::setResolution);
        }
    }

    /** Original / 720p / 480p / Auto. */
    private static void bindSeparated(AppCompatActivity activity, CastSettings settings, Spinner spinner) {
        CastSettings.Resolution[] modes = {
                CastSettings.Resolution.FULL,
                CastSettings.Resolution.HD_720P,
                CastSettings.Resolution.SD_480P,
                CastSettings.Resolution.ADAPTIVE,
        };
        String[] labels = {
                activity.getString(R.string.resolution_preset_original),
                activity.getString(R.string.resolution_preset_720p),
                activity.getString(R.string.resolution_preset_480p),
                activity.getString(R.string.resolution_preset_auto),
        };
        CastSettingsBinder.bindEnumSpinner(spinner, modes, labels, settings.getResolution(),
                settings::setResolution);
    }

    /** Fixed 480p + fixed 720p + native original + bandwidth adaptive. */
    private static void bindCombined(AppCompatActivity activity, CastSettings settings, Spinner spinner) {
        CastSettings.Resolution[] modes = {
                CastSettings.Resolution.SD_480P,
                CastSettings.Resolution.HD_720P,
                CastSettings.Resolution.FULL,
                CastSettings.Resolution.ADAPTIVE,
        };
        String[] labels = {
                activity.getString(R.string.resolution_preset_480p),
                activity.getString(R.string.resolution_preset_720p),
                activity.getString(R.string.resolution_combined_original),
                activity.getString(R.string.resolution_combined_adaptive),
        };
        CastSettingsBinder.bindEnumSpinner(spinner, modes, labels, settings.getResolution(),
                settings::setResolution);
    }

    /** Pre-refactor sender resolution list (subset previously in CastSettingsBinder). */
    private static void bindLegacy(AppCompatActivity activity, CastSettings settings, Spinner spinner) {
        CastSettings.Resolution[] modes = {
                CastSettings.Resolution.ADAPTIVE,
                CastSettings.Resolution.FIXED_1080P,
                CastSettings.Resolution.FIXED_720P,
                CastSettings.Resolution.FULL,
                CastSettings.Resolution.THREE_QUARTERS,
                CastSettings.Resolution.HALF,
                CastSettings.Resolution.HD_720P,
        };
        String[] labels = {
                activity.getString(R.string.resolution_adaptive),
                activity.getString(R.string.resolution_fixed_1080p),
                activity.getString(R.string.resolution_fixed_720p),
                activity.getString(R.string.resolution_full),
                activity.getString(R.string.resolution_75),
                activity.getString(R.string.resolution_half),
                activity.getString(R.string.resolution_720p),
        };
        CastSettingsBinder.bindEnumSpinner(spinner, modes, labels, settings.getResolution(),
                settings::setResolution);
    }
}
