/*
 * Copyright KG Soft
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */
package com.kgurgul.cpuinfo.features.information.cpu

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.kgurgul.cpuinfo.domain.model.CpuData
import com.kgurgul.cpuinfo.shared.Res
import com.kgurgul.cpuinfo.shared.cpu_abi
import com.kgurgul.cpuinfo.shared.cpu_cores
import com.kgurgul.cpuinfo.shared.cpu_current_frequency
import com.kgurgul.cpuinfo.shared.cpu_frequency_stopped
import com.kgurgul.cpuinfo.shared.cpu_has_neon
import com.kgurgul.cpuinfo.shared.cpu_l1d
import com.kgurgul.cpuinfo.shared.cpu_l1i
import com.kgurgul.cpuinfo.shared.cpu_l2
import com.kgurgul.cpuinfo.shared.cpu_l3
import com.kgurgul.cpuinfo.shared.cpu_l4
import com.kgurgul.cpuinfo.shared.cpu_soc_name
import com.kgurgul.cpuinfo.shared.no
import com.kgurgul.cpuinfo.shared.yes
import com.kgurgul.cpuinfo.ui.components.CpuDivider
import com.kgurgul.cpuinfo.ui.components.CpuProgressBar
import com.kgurgul.cpuinfo.ui.components.CpuPullToRefreshBox
import com.kgurgul.cpuinfo.ui.components.ItemValueRow
import com.kgurgul.cpuinfo.ui.components.VerticalScrollbar
import com.kgurgul.cpuinfo.ui.theme.CpuInfoTheme
import com.kgurgul.cpuinfo.ui.theme.spacingSmall
import com.kgurgul.cpuinfo.utils.formatHz
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel

@Composable
fun CpuInfoScreen(viewModel: CpuInfoViewModel = koinViewModel()) {
    val uiState by viewModel.uiStateFlow.collectAsStateWithLifecycle()
    CpuInfoScreen(uiState = uiState)
}

@Composable
fun CpuInfoScreen(uiState: CpuInfoViewModel.UiState) {
    CpuPullToRefreshBox(
        isRefreshing = uiState.isInitializing,
        onRefresh = {},
        enabled = false,
        modifier = Modifier.fillMaxSize(),
    ) {
        val listState = rememberLazyListState()
        LazyColumn(
            contentPadding = PaddingValues(spacingSmall),
            verticalArrangement = Arrangement.spacedBy(spacingSmall),
            state = listState,
            modifier = Modifier.fillMaxSize().testTag(CpuInfoScreenTestTags.LAZY_COLUMN),
        ) {
            uiState.cpuData?.let { cpuData ->
                item(key = "__soc_name") {
                    ItemValueRow(
                        title = stringResource(Res.string.cpu_soc_name),
                        value = cpuData.processorName,
                        modifier = Modifier.testTag(CpuInfoScreenTestTags.SOCKET_NAME),
                    )
                    Spacer(modifier = Modifier.requiredSize(spacingSmall))
                    CpuDivider()
                }
                item(key = "__abi") {
                    ItemValueRow(title = stringResource(Res.string.cpu_abi), value = cpuData.abi)
                    Spacer(modifier = Modifier.requiredSize(spacingSmall))
                    CpuDivider()
                }
                item(key = "__cores") {
                    ItemValueRow(
                        title = stringResource(Res.string.cpu_cores),
                        value = cpuData.coreNumber.toString(),
                    )
                    Spacer(modifier = Modifier.requiredSize(spacingSmall))
                    CpuDivider()
                }
                item(key = "__has_neon") {
                    ItemValueRow(
                        title = stringResource(Res.string.cpu_has_neon),
                        value =
                            if (cpuData.hasArmNeon) {
                                stringResource(Res.string.yes)
                            } else {
                                stringResource(Res.string.no)
                            },
                    )
                }
                if (cpuData.l1dCaches.isNotEmpty()) {
                    item(key = "__l1d") {
                        CpuDivider()
                        Spacer(modifier = Modifier.requiredSize(spacingSmall))
                        ItemValueRow(
                            title = stringResource(Res.string.cpu_l1d),
                            value = cpuData.l1dCaches,
                        )
                    }
                }
                if (cpuData.l1iCaches.isNotEmpty()) {
                    item(key = "__l1i") {
                        CpuDivider()
                        Spacer(modifier = Modifier.requiredSize(spacingSmall))
                        ItemValueRow(
                            title = stringResource(Res.string.cpu_l1i),
                            value = cpuData.l1iCaches,
                        )
                    }
                }
                if (cpuData.l2Caches.isNotEmpty()) {
                    item(key = "__l2") {
                        CpuDivider()
                        Spacer(modifier = Modifier.requiredSize(spacingSmall))
                        ItemValueRow(
                            title = stringResource(Res.string.cpu_l2),
                            value = cpuData.l2Caches,
                        )
                    }
                }
                if (cpuData.l3Caches.isNotEmpty()) {
                    item(key = "__l3") {
                        CpuDivider()
                        Spacer(modifier = Modifier.requiredSize(spacingSmall))
                        ItemValueRow(
                            title = stringResource(Res.string.cpu_l3),
                            value = cpuData.l3Caches,
                        )
                    }
                }
                if (cpuData.l4Caches.isNotEmpty()) {
                    item(key = "__l4") {
                        CpuDivider()
                        Spacer(modifier = Modifier.requiredSize(spacingSmall))
                        ItemValueRow(
                            title = stringResource(Res.string.cpu_l4),
                            value = cpuData.l4Caches,
                        )
                    }
                }
                cpuData.frequencies.forEachIndexed { i, frequency ->
                    item(key = "__frequency_$i") {
                        if (i == 0) {
                            CpuDivider()
                            Spacer(modifier = Modifier.requiredSize(spacingSmall))
                        }
                        FrequencyItem(index = i, frequency = frequency)
                    }
                }
            }
        }
        VerticalScrollbar(
            modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight(),
            scrollState = listState,
        )
    }
}

@Composable
fun FrequencyItem(index: Int, frequency: CpuData.Frequency) {
    val currentFreq =
        if (frequency.current != -1L) {
            stringResource(Res.string.cpu_current_frequency, index, formatHz(frequency.current))
        } else {
            stringResource(Res.string.cpu_frequency_stopped, index)
        }
    val minFreq =
        if (frequency.min != -1L) {
            formatHz(0)
        } else {
            ""
        }
    val maxFreq =
        if (frequency.max != -1L) {
            formatHz(frequency.max)
        } else {
            ""
        }
    val progress =
        if (frequency.current != -1L && frequency.max != 0L) {
            frequency.current.toFloat() / frequency.max.toFloat()
        } else {
            0f
        }
    CpuProgressBar(label = currentFreq, progress = progress, minMaxValues = minFreq to maxFreq)
}

object CpuInfoScreenTestTags {
    const val LAZY_COLUMN = "cpu_info_lazy_column"
    const val SOCKET_NAME = "cpu_info_socket_name"
}

@Preview
@Composable
fun CpuInfoScreenPreview() {
    CpuInfoTheme {
        CpuInfoScreen(
            uiState =
                CpuInfoViewModel.UiState(
                    cpuData =
                        CpuData(
                            processorName = "processorName",
                            abi = "abi",
                            coreNumber = 1,
                            hasArmNeon = true,
                            frequencies = listOf(CpuData.Frequency(min = 1, max = 2, current = 3)),
                            l1dCaches = "l1dCaches",
                            l1iCaches = "l1iCaches",
                            l2Caches = "l2Caches",
                            l3Caches = "l3Caches",
                            l4Caches = "l4Caches",
                        )
                )
        )
    }
}
