package com.za.ui.servicing.destination_photo

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.za.base.BaseActivity
import com.za.base.view.CommonButton
import com.za.base.view.CommonDialog
import com.za.bean.db.order.PhotoTemplateInfo
import com.za.ext.finish
import com.za.ext.goNextPage
import com.za.ui.servicing.InServicingPhotoView
import com.za.ui.servicing.view.InServicingHeadView

class DestinationPhotoActivity : BaseActivity() {
    @Composable
    override fun ContentView() {
        DestinationPhotoScreen()
    }
}

@Composable
fun DestinationPhotoScreen(vm: DestinationPhotoVm = viewModel()) {
    val uiState = vm.uiState.collectAsStateWithLifecycle()
    val context = LocalContext.current

    LaunchedEffect(key1 = Unit) {
        vm.dispatch(DestinationPhotoVm.Action.Init)
    }

    if (uiState.value.goNextPage != null) {
        goNextPage(uiState.value.goNextPage?.nextState, context)
    }

    if (uiState.value.isGoNextPageDialog == true) {
        CommonDialog(
            cancelText = "取消",
            confirmText = "前往下一步",
            title = "是否前往下一步？",
            cancelEnable = true,
            cancel = {
                vm.dispatch(DestinationPhotoVm.Action.UpdateState(uiState.value.copy(isGoNextPageDialog = false)))
            },
            dismiss = { vm.dispatch(DestinationPhotoVm.Action.UpdateState(uiState.value.copy(isGoNextPageDialog = false))) },
            confirm = {
                vm.dispatch(DestinationPhotoVm.Action.UpdateState(uiState.value.copy(isGoNextPageDialog = false)))
                vm.dispatch(DestinationPhotoVm.Action.UpdateTask)
            })
    }

    //离线操作框
    if (uiState.value.showOfflineDialog == true) {
        CommonDialog(
            cancelText = "取消",
            confirmText = "离线上传",
            title = "任务提交失败，是否离线提交？",
            cancelEnable = true,
            cancel = {
                vm.dispatch(DestinationPhotoVm.Action.UpdateState(uiState.value.copy(showOfflineDialog = false)))
            },
            dismiss = { vm.dispatch(DestinationPhotoVm.Action.UpdateState(uiState.value.copy(showOfflineDialog = false))) },
            confirm = {
                vm.dispatch(DestinationPhotoVm.Action.UpdateState(uiState.value.copy(showOfflineDialog = false)))
                vm.dispatch(DestinationPhotoVm.Action.UpdateOffline)
            })
    }

    Scaffold(topBar = {
        InServicingHeadView(title = "到达目的地照片",
            orderInfo = uiState.value.orderInfo,
            onBack = { context.finish() })
    }, bottomBar = {
        CommonButton(text = "上传照片") {
            vm.dispatch(DestinationPhotoVm.Action.UpdateState(uiState.value.copy(isGoNextPageDialog = true)))
        }
    }) { it ->
        LazyColumn(modifier = Modifier
            .fillMaxSize()
            .padding(it),
            contentPadding = PaddingValues(10.dp)) {
            itemsIndexed(items = uiState.value.photoTemplateList
                ?: arrayListOf(), key = { _, item -> item.hashCode() }) { index: Int, item: PhotoTemplateInfo ->
                InServicingPhotoView(photoTemplateInfo = item, index = index + 1, success = {
                    vm.dispatch(DestinationPhotoVm.Action.UpdatePhotoTemplate(it))
                })
                Spacer(modifier = Modifier.height(10.dp))
            }
        }
    }
}
