package com.example.projectbnspumkm.ui

import androidx.lifecycle.*
import com.example.projectbnspumkm.data.model.CartItem
import com.example.projectbnspumkm.data.model.Customer
import com.example.projectbnspumkm.data.model.Product
import com.example.projectbnspumkm.data.repository.Resource
import com.example.projectbnspumkm.data.repository.UmkmRepository
import kotlinx.coroutines.launch

class MainViewModel(private val repository: UmkmRepository) : ViewModel() {

    val products: LiveData<Resource<List<Product>>> = repository.getProducts().asLiveData()

    val customers: LiveData<Resource<List<Customer>>> = repository.getCustomers().asLiveData()

    val cartItems: LiveData<List<CartItem>> = repository.getCartItems().asLiveData()

    init {
        seedDummyData()
        createCustomers()
    }

    private fun seedDummyData() {
        viewModelScope.launch {
            repository.getProducts().collect { resource ->
                if (resource is Resource.Success && resource.data.isNullOrEmpty()) {
                    val dummyProducts = listOf(
                        Product(
                            1,
                            "Kripik Singkong",
                            "Renyah dan gurih",
                            15000.0,
                            "https://i.pinimg.com/736x/d4/97/85/d497853360bab7206594e511bdd56dc0.jpg"
                        ),
                        Product(
                            2,
                            "Basreng Pedas",
                            "Bakso goreng bumbu pedas",
                            20000.0,
                            "https://i.pinimg.com/736x/44/43/a6/4443a6ea1688b38af3be677c0c247600.jpg"
                        ),
                        Product(
                            3,
                            "Roti Bakar",
                            "Roti bakar aneka rasa",
                            25000.0,
                            "https://i.pinimg.com/1200x/eb/3c/e9/eb3ce9d082f91d6725b5d644cd159a0a.jpg"
                        ),
                        Product(
                            4,
                            "Es Teh Manis",
                            "Segar dan nikmat",
                            5000.0,
                            "https://i.pinimg.com/736x/0a/7c/27/0a7c27f56aa886c3451e4cf97bc37e57.jpg"
                        ),
                        Product(
                            5,
                            "Martabak telur",
                            "Martabak dengan gurih asin mantap",
                            10000.0,
                            "https://i.pinimg.com/736x/89/0b/9b/890b9bfc279bdaea29f14858599764b1.jpg"
                        ),
                        Product(
                            6,
                            "Makaroni",
                            "Rasanya gurih dan pedas",
                            10000.0,
                            "https://i.pinimg.com/1200x/1a/8c/e7/1a8ce7674c08226e67ffe5f34c277738.jpg"
                        ),
                        Product(
                            7,
                            "Kopi Susu",
                            "Kopi manis penambah semangat",
                            4000.0,
                            "https://i.pinimg.com/736x/15/84/12/158412ceb984df7b4b48e78d2e535345.jpg"
                        ),
                        Product(
                            8,
                            "Tahu bulat",
                            "Rasanya gurih murah lagi",
                            500.0,
                            "https://i.pinimg.com/1200x/7b/e9/13/7be91357b4836b1b2b6cd67d4b271c44.jpg"
                        ),
                        Product(
                            9,
                            "Ketoprak",
                            "Cocok dimakan bareng keluarga",
                            12000.0,
                            "https://i.pinimg.com/736x/71/20/24/7120249d7e007134f8769623651cca42.jpg"
                        ),
                        Product(
                            10,
                            "Asinan",
                            "Rasanya pedas manis",
                            5000.0,
                            "https://i.pinimg.com/736x/96/dd/18/96dd180859ce81f967c67a4eca4b3fa4.jpg"
                        )
                    )
                    repository.insertProducts(dummyProducts)
                }
            }
        }
    }

    fun getProductById(id: Int): LiveData<Product?> = repository.getProductById(id).asLiveData()

    fun removeProduct(product: Product) {
        viewModelScope.launch {
            repository.removeProduct(product)
        }
    }

    fun addToCart(product: Product) {
        viewModelScope.launch {
            val item = CartItem(
                productId = product.id,
                quantity = 1,
                productName = product.name,
                productPrice = product.price
            )
            repository.addToCart(item)
        }
    }

    fun removeFromCart(item: CartItem) {
        viewModelScope.launch {
            repository.removeFromCart(item)
        }
    }

    fun addProduct(product: Product) {
        viewModelScope.launch {
            repository.addProduct(product)
        }
    }

    fun updateProduct(product: Product) {
        viewModelScope.launch {
            repository.updateProduct(product)
        }
    }

    fun addCustomer(customer: Customer) {
        viewModelScope.launch {
            repository.addCustomer(customer)
        }
    }

    fun removeCustomers(customer: Customer) {
        viewModelScope.launch {
            repository.removeCustomer(customer)
        }
    }

    fun updateCustomers(customer: Customer) {
        viewModelScope.launch {
            repository.updateCustomers(customer)
        }
    }

    fun createCustomers() {
        val dummyCustomers = listOf(
            Customer(1, "John Doe", "john@gmail.com", "085628929102"),
            Customer(2, "Jane Smith", "jane@gmail.com", "089203492541"),
            Customer(3, "Bob Johnson", "bob@gmail.com", "081234567890"),
            Customer(4, "Alice Brown", "alice@gmail.com", "087654321098"),
            Customer(5, "Charlie Wilson", "charlie@gmail.com", "082345678901"),
            Customer(6, "Eva Davis", "eva@gmail.com", "089876543210"),
            Customer(7, "David Lee", "david@gmail.com", "083456789012"),
            Customer(8, "Grace Miller", "grace@gmail.com", "086543210987"),
            Customer(9, "Frank Turner", "frank@gmail.com", "084567890123"),
            Customer(10, "Helen Clark", "helen@gmail.com", "081234567890")
        )
        viewModelScope.launch {
            repository.insertCustomers(dummyCustomers)
        }
    }

    class Factory(private val repository: UmkmRepository) : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            if (modelClass.isAssignableFrom(MainViewModel::class.java)) {
                @Suppress("UNCHECKED_CAST")
                return MainViewModel(repository) as T
            }
            throw IllegalArgumentException("Unknown ViewModel class")
        }
    }
}
