package alraune

import vgrechka.*
import alraune.Al.*
import alraune.entity.*
import pieces100.*

@Suppress("unused") // a1aea2f1-07f7-4ce4-95d0-aa9467eb4a40
class FileField__killme : FormField<AlFileFormValue> {
    val name = AlConst.FTBDataKeys.file
    private var _value: AlFileFormValue? = null
    private var error: String? = null

    override fun use(source: FieldSource) {
        exhaustive=when (source) {
            is FieldSource.Initial -> {
                _value = AlFileFormValue().fill(meat = null)
                error = null
            }

            is FieldSource.Post -> {
                val json = rctx0.ftb.data!!.get(name) as String
                val value = bang(dejsonize(AlFileFormValue::class.java, json))
                _value = value

                cond(value.meat == null) {
                    error = t("TOTE", "Выбери файл, ОК?")
                }
            }

            is FieldSource.DB -> {
                val entity = source.entity
                _value = when (entity) {
                    is AlUAOrderFile -> {
                        val resource = entity.dataPile.resource
                        AlFileFormValue().fill(
                            meat = AlFileFormValue.Meat().fill(
                                changed = false,
                                name = resource.name,
                                size = resource.size,
                                downloadUrl = resource.downloadUrl,
                                secretKeyBase64 = resource.secretKeyBase64))
                    }

                    is Order.File -> {
                        val resource = entity.resource
                        AlFileFormValue().fill(
                            meat = AlFileFormValue.Meat().fill(
                                changed = false,
                                name = resource.name,
                                size = resource.size,
                                downloadUrl = resource.downloadUrl,
                                secretKeyBase64 = resource.secretKeyBase64))
                    }

                    else -> wtf()
                }
            }
        }
    }

    override fun name() = name
    override fun error() = error

    override fun value(): AlFileFormValue {
        checkFieldUsed(this)
        return _value!!
    }

    override fun used() = _value != null

    override fun setName(name: String) {
        // Name is fixed, at least for now
    }

    override fun debug_shitIntoBogusRequestModel_asIfItCameFromClient(model: BogusRequestModel) {
        model.data.put(name, jsonize(value()))
    }

    override fun update(entity: Any) {
        val meat = value().meat!!
        if (meat.changed) {
            exhaustive=when (entity) {
                is AlUAOrderFile -> {
                    entity.dataPile.resource = AlUAOrderFile.Resource().fill(
                        name = meat.name,
                        size = meat.size,
                        downloadUrl = meat.downloadUrl,
                        secretKeyBase64 = meat.secretKeyBase64)
                }

                is Order.File -> {
                    entity.resource = Order.Resource().fill(
                        name = meat.name,
                        size = meat.size,
                        downloadUrl = meat.downloadUrl,
                        secretKeyBase64 = meat.secretKeyBase64)
                }

                else -> wtf()
            }
        }
    }

    fun compose() = FilePicker(value(), error)

    fun set(x: AlFileFormValue) {
        _value = x
    }

    override fun isOnlyForAdmin() = false
}





