package com.example.myapplication.api.rent.repository

import android.util.Log
import androidx.paging.ExperimentalPagingApi
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.example.myapplication.api.MyServerService
import com.example.myapplication.api.bike.remote.toBikeRemote
import com.example.myapplication.api.item.remote.toItemFromRent
import com.example.myapplication.api.rent.mediator.RentRemoteMediator
import com.example.myapplication.api.rent.remote.toRent
import com.example.myapplication.api.rent.remote.toRentRemote
import com.example.myapplication.database.room.repository.AppContainer
import com.example.myapplication.database.AppDatabase
import com.example.myapplication.database.remotekeys.repository.offline.OfflineRemoteKeyRepository
import com.example.myapplication.database.room.model.item.ItemFromRent
import com.example.myapplication.database.room.model.rent.Rent
import com.example.myapplication.database.room.model.rent.RentItemCrossRef
import com.example.myapplication.database.room.repository.offline.OfflineBikeRepository
import com.example.myapplication.database.room.repository.offline.OfflineRentItemRepository
import com.example.myapplication.database.room.repository.offline.OfflineRentRepository
import com.example.myapplication.database.room.repository.online.RentRepository
import kotlinx.coroutines.flow.Flow

class RestRentRepository(
    private val service: MyServerService,
    private val dbRentRepository: OfflineRentRepository,
    private val dbBikeRepository: OfflineBikeRepository,
    private val dbRentItemRepository: OfflineRentItemRepository,
    private val dbRemoteKeyRepository: OfflineRemoteKeyRepository,
    private val database: AppDatabase
) : RentRepository {
    override fun getAllRents(userId: Int?): Flow<PagingData<Rent>> {
        Log.d(RestRentRepository::class.simpleName, "Get rents")

        val pagingSourceFactory = { dbRentRepository.getAllRentsPagingSource(userId) }

        @OptIn(ExperimentalPagingApi::class)
        return Pager(
            config = PagingConfig(
                pageSize = AppContainer.LIMIT,
                enablePlaceholders = false
            ),
            remoteMediator = RentRemoteMediator(
                service,
                dbRentRepository,
                dbRemoteKeyRepository,
                database,
            ),
            pagingSourceFactory = pagingSourceFactory
        ).flow
    }

    override suspend fun getRent(uid: Int): List<ItemFromRent> {
        val rent = service.getRent(uid)

        dbRentItemRepository.deleteRentItems(uid)
        rent.items.map {
            dbRentItemRepository.insertRentItem(
                RentItemCrossRef(
                    uid,
                    it.id,
                    it.frozenWeight,
                    it.radius,
                    it.colorbike,
                    it.count
                )
            )
        }
        return rent.items.map { x -> x.toItemFromRent(dbBikeRepository.getBike(x.bikeId).bike.toBikeRemote()) }
    }

    override suspend fun insertRent(rent: Rent): Long {
        return dbRentRepository.insertRent(service.createRent(rent.toRentRemote()).toRent())
    }

    override suspend fun deleteRent(rent: Rent) {
        service.deleteRent(rent.uid)
        dbRentRepository.deleteRent(rent)
    }
}