package com.iscs.bozzys.ui.pages.create.job

import android.content.Context
import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
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.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.iscs.bozzys.R
import com.iscs.bozzys.event.RefreshEvent
import com.iscs.bozzys.event.RefreshEventBus
import com.iscs.bozzys.ui.common.PageBase
import com.iscs.bozzys.ui.common.Title
import com.iscs.bozzys.ui.pages.compose.CardBox
import com.iscs.bozzys.ui.pages.compose.FormBox
import com.iscs.bozzys.ui.pages.edit.step.openPageEditStep
import com.iscs.bozzys.ui.pages.vm.VMCreateJob

fun Context.openPageCreateJob(id: Int) {
    startActivity(Intent(this, PageCreateJob::class.java).apply {
        putExtra("id", id)
    })
}

class PageCreateJob : PageBase() {

    @Composable
    override fun GetViews(pv: PaddingValues) {
        val id = intent.getIntExtra("id", 0)
        val vm: VMCreateJob = viewModel()
        val state by vm.state.collectAsState()
        LaunchedEffect(Unit) {
            vm.toast.initToast()
            vm.loading.initLoading()
            vm.init(id)
        }
        Column(
            Modifier
                .fillMaxSize()
                .background(Color(0xFFF8F9FA))
        ) {
            Title(pv, if (id > 0) "编辑作业" else "新建作业")
            Column(
                modifier = Modifier
                    .weight(1f)
                    .verticalScroll(state = rememberScrollState())
            ) {
                // 表单内容
                if (state.forms.isNotEmpty()) CardBox(Modifier.padding(horizontal = 16.dp, vertical = 16.dp)) {
                    FormBox(
                        state.forms, {}, modifier = Modifier
                            .padding(horizontal = 10.dp)
                            .padding(top = 5.dp, bottom = 16.dp)
                    )
                }
            }
            // 底部下一步按钮
            Column(
                Modifier
                    .fillMaxWidth()
                    .background(MaterialTheme.colorScheme.background)
                    .padding(top = 15.dp, bottom = (15f + pv.calculateBottomPadding().value).dp)
            ) {
                Button(
                    onClick = {
                        vm.onNext {
                            // 通知首页刷新数据
                            RefreshEventBus.onRefreshData(RefreshEvent.JobDetail)
                            RefreshEventBus.onRefreshData(RefreshEvent.HomeData)
                            openPageEditStep(it)
                            destroyDelay(500)
                        }
                    }, modifier = Modifier
                        .padding(horizontal = 16.dp)
                        .fillMaxWidth()
                        .height(50.dp)
                        .clip(RoundedCornerShape(12.dp))
                        .background(MaterialTheme.colorScheme.primary),
                    shape = RoundedCornerShape(12.dp)
                ) {
                    Row(verticalAlignment = Alignment.CenterVertically) {
                        Text("下一步", fontSize = 16.sp, fontWeight = FontWeight.Bold)
                        Icon(
                            painter = painterResource(R.drawable.next),
                            contentDescription = "",
                            modifier = Modifier
                                .padding(start = 5.dp)
                                .size(16.dp)
                        )
                    }
                }
            }
        }
    }
}