package com.zaojiao.app.data.local.baby

import androidx.datastore.core.DataStore
import com.zaojiao.app.data.local.BabyPreferences
import com.zaojiao.app.data.local.BabyPreferences.BabyItemPreferences
import com.zaojiao.app.data.local.copy
import com.zaojiao.app.data.model.BabyModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class LocalBabyData @Inject constructor(
    private val babyPreferences: DataStore<BabyPreferences>,
) {
    val current: Flow<BabyModel?> = babyPreferences.data.map {
        it.itemsList
            .firstOrNull { e -> e.isSelect == 1 }
            ?.let { p ->
                BabyModel(
                    id = p.id,
                    img = p.img,
                    gender = p.gender,
                    name = p.name,
                    birthday = p.birthday,
                    relation = p.relation,
                    relationName = p.relationName,
                    age = p.age,
                    isSelect = p.isSelect,
                )
            }
    }

    val list: Flow<List<BabyModel>> = babyPreferences.data.map {
        it.itemsList.map { item ->
            BabyModel(
                id = item.id,
                img = item.img,
                gender = item.gender,
                name = item.name,
                birthday = item.birthday,
                relation = item.relation,
                relationName = item.relationName,
                age = item.age,
                isSelect = item.isSelect,
            )
        }
    }

    suspend fun setSelect(index: Int) {
        babyPreferences.updateData {
            val list = it.itemsList.mapIndexed { i, item ->
                val builder = BabyItemPreferences.newBuilder(item)
                if (i == index) {
                    builder.isSelect = 1
                } else {
                    builder.isSelect = 0
                }
                builder.build()
            }

            it.copy {
                items.clear()
                items.addAll(list)
            }
        }
    }

    suspend fun setSelect(id: String) {
        babyPreferences.updateData {
            val list = it.itemsList.map { item ->
                val builder = BabyItemPreferences.newBuilder(item)
                if (id == item.id) {
                    builder.isSelect = 1
                } else {
                    builder.isSelect = 0
                }
                builder.build()
            }

            it.copy {
                items.clear()
                items.addAll(list)
            }
        }
    }

    suspend fun updateList(list: List<BabyModel>) {
        val protobufList = list.map { item ->
            BabyItemPreferences.newBuilder().run {
                id = item.id
                img = item.img
                gender = item.gender
                name = item.name
                birthday = item.birthday
                relation = item.relation
                relationName = item.relationName
                age = item.age
                isSelect = item.isSelect
                build()
            }
        }

        babyPreferences.updateData {
            it.copy {
                items.clear()
                items.addAll(protobufList)
            }
        }
    }
}