/*
 * ImageToolbox is an image editor for android
 * Copyright (c) 2026 T8RIN (Malik Mukhametzyanov)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * You should have received a copy of the Apache License
 * along with this program.  If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
 */

package com.t8rin.imagetoolbox.feature.recognize.text.presentation.components

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import com.t8rin.imagetoolbox.core.domain.utils.humanFileSize
import com.t8rin.imagetoolbox.core.resources.Icons
import com.t8rin.imagetoolbox.core.resources.R
import com.t8rin.imagetoolbox.core.resources.icons.WifiTetheringError
import com.t8rin.imagetoolbox.core.ui.utils.helper.AppToastHost
import com.t8rin.imagetoolbox.core.ui.widget.other.ToastDuration
import com.t8rin.imagetoolbox.core.utils.getString
import com.t8rin.imagetoolbox.feature.recognize.text.domain.RecognitionType
import com.t8rin.imagetoolbox.feature.recognize.text.presentation.screenLogic.RecognizeTextComponent

@Composable
internal fun RecognizeTextDownloadDataDialog(component: RecognizeTextComponent) {
    val downloadDialogData = component.downloadDialogData

    if (component.showPaddleDownloadDialog) {
        var progress by rememberSaveable(Unit) {
            mutableFloatStateOf(0f)
        }
        var dataRemaining by rememberSaveable(Unit) {
            mutableStateOf("")
        }
        DownloadLanguageDialog(
            title = R.string.paddle_ocr,
            description = R.string.download_paddle_ocr_description,
            descriptionArgs = arrayOf(component.paddleOCRModel.englishName),
            onDownloadRequest = {
                component.downloadPaddleData(
                    onProgress = { (p, size) ->
                        dataRemaining = humanFileSize(size)
                        progress = p
                    },
                    onComplete = {
                        AppToastHost.showConfetti()
                        component.startRecognition()
                    }
                )
            },
            downloadProgress = progress,
            dataRemaining = dataRemaining,
            onNoConnection = {
                component.clearPaddleDownloadDialog()
                AppToastHost.showToast(
                    message = getString(R.string.no_connection),
                    icon = Icons.Rounded.WifiTetheringError,
                    duration = ToastDuration.Long
                )
            },
            onDismiss = component::clearPaddleDownloadDialog
        )
    }

    if (downloadDialogData.isNotEmpty()) {
        var progress by rememberSaveable(downloadDialogData) {
            mutableFloatStateOf(0f)
        }
        var dataRemaining by rememberSaveable(downloadDialogData) {
            mutableStateOf("")
        }
        DownloadLanguageDialog(
            downloadDialogData = downloadDialogData,
            onDownloadRequest = { downloadData ->
                component.downloadTrainData(
                    type = downloadData.firstOrNull()?.type
                        ?: RecognitionType.Standard,
                    languageCode = downloadDialogData.joinToString(separator = "+") { it.languageCode },
                    onProgress = { (p, size) ->
                        dataRemaining = humanFileSize(size)
                        progress = p
                    },
                    onComplete = {
                        component.clearDownloadDialogData()
                        AppToastHost.showConfetti()
                        component.startRecognition()
                    }
                )
            },
            downloadProgress = progress,
            dataRemaining = dataRemaining,
            onNoConnection = {
                component.clearDownloadDialogData()
                AppToastHost.showToast(
                    message = getString(R.string.no_connection),
                    icon = Icons.Rounded.WifiTetheringError,
                    duration = ToastDuration.Long
                )
            },
            onDismiss = component::clearDownloadDialogData
        )
    }
}