/* ncurses part */

-- ncurses charset
CREATE VIEW IF NOT EXISTS charsym AS
SELECT num AS id, CHAR(num) AS c 
FROM enum256 
WHERE num >= UNICODE(' ') 
AND num <= UNICODE('~');

-- ncurses color enum
CREATE VIEW IF NOT EXISTS color AS
    SELECT 0 AS 'id' ,'BLACK' AS 'name'
    UNION SELECT 1,'RED'
    UNION SELECT 2,'GREEN'
    UNION SELECT 3,'YELLOW'
    UNION SELECT 4,'BLUE'
    UNION SELECT 5,'MAGENTA'
    UNION SELECT 6,'CYAN'
    UNION SELECT 7,'WHITE';

CREATE TABLE IF NOT EXISTS animation (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS frame (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    animationid INTEGER REFERENCES animation(id) NULL,
    charsymid INTEGER REFERENCES charsym(id) NOT NULL,
    foregroundid INTEGER REFERENCES color(id) DEFAULT 7,
    backgroundid INTEGER REFERENCES color(id) DEFAULT 0
);
INSERT INTO frame (name,charsymid) 
SELECT 'oak',charsym.id FROM charsym WHERE charsym.c = 't';

CREATE VIEW IF NOT EXISTS framed AS
    SELECT frame.id AS id,
           frame.animationid AS animationid,
           charsym.c AS charsym,
           charsymid,
           foreground.name AS foreground,
           foregroundid,
           background.name AS background,
           backgroundid
    FROM frame
    INNER JOIN charsym ON charsym.id = charsymid
    INNER JOIN color AS foreground ON foreground.id = foregroundid
    INNER JOIN color AS background ON background.id = backgroundid;

CREATE VIEW IF NOT EXISTS animated AS
    SELECT animation.id AS id,
           animation.name AS name
    FROM animation
    INNER JOIN framed ON animationid = animation.id;
