package com.gmplayer.app

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

enum class MobilePlaybackSideControlsMode {
    Skip15,
    Playlist,
    Hidden
}

@Composable
fun MobileSkip15Button(
    forward: Boolean,
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {}
) {
    Box(
        modifier = modifier
            .size(66.dp)
            .clip(CircleShape)
            .background(Color.Black.copy(alpha = 0.25f))
            .clickable(role = Role.Button, onClick = onClick)
    ) {
        Image(
            painter = painterResource(
                id = if (forward) {
                    R.drawable.player_button_go_forward_15
                } else {
                    R.drawable.player_button_go_backwards_15
                }
            ),
            contentDescription = if (forward) "Forward 15 seconds" else "Back 15 seconds",
            modifier = Modifier.size(66.dp)
        )
    }
}

@Composable
fun MobileSkipVideoButton(
    forward: Boolean,
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {}
) {
    Box(
        modifier = modifier
            .size(66.dp)
            .clip(CircleShape)
            .background(Color.Black.copy(alpha = 0.25f))
            .clickable(role = Role.Button, onClick = onClick)
    ) {
        Image(
            painter = painterResource(
                id = if (forward) {
                    R.drawable.player_button_skip_forward
                } else {
                    R.drawable.player_button_skip_backward
                }
            ),
            contentDescription = if (forward) "Next video" else "Previous video",
            modifier = Modifier.size(66.dp)
        )
    }
}

@Composable
fun MobilePlaybackControls(
    state: MobileMainControllerState,
    modifier: Modifier = Modifier,
    sideControlsMode: MobilePlaybackSideControlsMode = PlayerControlsConfiguration.sideControlsMode,
    onPrevious: () -> Unit = {},
    onBack15: () -> Unit = {},
    onMainClick: () -> Unit = {},
    onForward15: () -> Unit = {},
    onNext: () -> Unit = {}
) {
    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {
        when (sideControlsMode) {
            MobilePlaybackSideControlsMode.Skip15 -> {
                MobileSkip15Button(forward = false, onClick = onBack15)
                Spacer(modifier = Modifier.width(16.dp))
            }
            MobilePlaybackSideControlsMode.Playlist -> {
                MobileSkipVideoButton(forward = false, onClick = onPrevious)
                Spacer(modifier = Modifier.width(16.dp))
            }
            MobilePlaybackSideControlsMode.Hidden -> Unit
        }

        MobileMainControllerButton(state = state, onClick = onMainClick)

        when (sideControlsMode) {
            MobilePlaybackSideControlsMode.Skip15 -> {
                Spacer(modifier = Modifier.width(16.dp))
                MobileSkip15Button(forward = true, onClick = onForward15)
            }
            MobilePlaybackSideControlsMode.Playlist -> {
                Spacer(modifier = Modifier.width(16.dp))
                MobileSkipVideoButton(forward = true, onClick = onNext)
            }
            MobilePlaybackSideControlsMode.Hidden -> Unit
        }
    }
}

@Preview(showBackground = true, backgroundColor = 0xFF20242C)
@Composable
private fun MobilePlaybackControlsPreview() {
    MobilePlaybackControls(state = MobileMainControllerState.Play)
}
