use crate::lang::{DeckSettings, OrderMode};

use rusqlite::Connection;

pub fn load_sets(connection: &Connection) -> Vec<DeckSettings> {
    let mut stmt = connection
        .prepare("SELECT id, name, forward, backward, filter FROM card_set")
        .unwrap();
    let iter = stmt
        .query_map([], |row| {
            Ok(DeckSettings {
                id: row.get(0)?,
                name: row.get(1)?,
                forward: row.get(2)?,
                backward: row.get(3)?,
                filter: row.get(4)?,
                count: None,
                worst_words_list: None,
                open_mode: OrderMode::Default,
            })
        })
        .unwrap();

    let mut buffer = vec![];
    for word in iter {
        buffer.push(word.unwrap());
    }

    buffer
}

pub fn add_set(set: &mut DeckSettings, connection: &Connection) {
    let index : u32 = connection
        .query_row(
            "INSERT INTO card_set (name, forward, backward, filter) VALUES (?1, ?2, ?3, ?4) RETURNING id",
            (
                &set.name,
                &set.forward,
                &set.backward,
                &set.filter,
            ),
            |row| row.get(0)
        )
        .unwrap();

    set.id = index.into();
}

pub fn update_deck(set: &mut DeckSettings, connection: &Connection) {
    if !set.id.is_valid() {
        add_set(set, connection);
    } else {
        connection
            .execute(
                "UPDATE card_set SET name = ?1, forward = ?2, backward = ?3, filter = ?4 WHERE id = ?5",
                (
                    &set.name,
                    &set.forward,
                    &set.backward,
                    &set.filter,
                    &set.id
                ),
            )
            .unwrap();
    }
}

pub fn delete_set(set: &DeckSettings, connection: &Connection) {
    if !set.id.is_valid() {
        return;
    }
    connection
        .execute("DELETE FROM card_set WHERE id = ?1", (&set.id,))
        .unwrap();
}
