/*
 *  neercs        console-based window manager
 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
 *                All Rights Reserved
 *
 *  $Id: effects.c 2360 2008-06-11 16:23:46Z jylam $
 *
 *  This program is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What The Fuck You Want
 *  To Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cucul.h>
#include <caca.h>
#include <config.h>
#include <time.h>

#include "neercs.h"


void draw_thumbnails(cucul_canvas_t *cv, struct screen_list *screen_list, int pty)
{
                char const * const *fonts;
                cucul_dither_t *d;
                cucul_font_t *f;
                uint8_t *buf;
                int i;
                int miniw, minih;

                if(screen_list->count)
                {
                    fonts = cucul_get_font_list();
                    f = cucul_load_font(fonts[0], 0);

                    miniw = cucul_get_canvas_width(screen_list->screen[0]->cv)
                        * cucul_get_font_width(f);
                    minih = cucul_get_canvas_height(screen_list->screen[0]->cv)
                        * cucul_get_font_height(f);
                    buf = malloc(4 * miniw * minih);

#if defined(HAVE_ENDIAN_H)
                    if(__BYTE_ORDER == __BIG_ENDIAN)
#else
                        /* This is compile-time optimised with at least -O1 or -Os */
                        uint32_t const tmp = 0x12345678;
                    if(*(uint8_t const *)&tmp == 0x12)
#endif
                        d = cucul_create_dither(32, miniw, minih, 4 * miniw,
                                                0xff0000, 0xff00, 0xff, 0x0);
                    else
                        d = cucul_create_dither(32, miniw, minih, 4 * miniw,
                                                0xff00, 0xff0000, 0xff000000, 0x0);

                    for(i = 0; i < screen_list->count; i++)
                    {
                        cucul_render_canvas(screen_list->screen[i]->cv, f, buf,
                                            miniw, minih, miniw * 4);
                        cucul_dither_bitmap(cv, 20 * i,
                                            cucul_get_canvas_height(cv) - 6, 19, 6, d, buf);
                        cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);

                        if(pty == i)
                            cucul_draw_cp437_box(cv,20 * i,
                                                 cucul_get_canvas_height(cv) - 6, 19, 6);
                        cucul_printf(cv, 20 * i,
                                     cucul_get_canvas_height(cv) - 6, "(%i)", i+1);
                    }

                    cucul_free_dither(d);
                    cucul_free_font(f);

                    free(buf);
                }

}

/* FIXME, make this stuff more configurable */
void draw_status(cucul_canvas_t *cv, struct screen_list *screen_list, int pty)
{
    int x = 0, y = cucul_get_canvas_height(cv) - 1;


        cucul_set_color_ansi(cv, CUCUL_BLUE, CUCUL_BLUE);
        cucul_fill_box(cv,
                       x, y,
                       cucul_get_canvas_width(cv), 1, '#');

/* Hour */
    {
        time_t now = time ((time_t *) 0);
        struct tm *t = localtime(&now);
        char hour[256];
        sprintf(hour, "%02d:%02d", t->tm_hour, t->tm_min);

        cucul_set_color_ansi(cv, CUCUL_LIGHTBLUE, CUCUL_BLUE);
        cucul_printf(cv, x, y,
                     "[");

        cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_BLUE);
        cucul_printf(cv, x+1, y,
                     hour);
        cucul_set_color_ansi(cv, CUCUL_LIGHTBLUE, CUCUL_BLUE);
        cucul_printf(cv, x + strlen(hour) + 1, y,
                     "]");
        x += 7;

    }

/* Window */
    {
        char text[256];
        sprintf(text, "%d/%d", pty+1, screen_list->count);
        x++;
        cucul_set_color_ansi(cv, CUCUL_LIGHTBLUE, CUCUL_BLUE);
        cucul_printf(cv, x, y, "Window:");
        cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_BLUE);
        cucul_printf(cv, x+8, y, text);
        x+= 8+strlen(text);
    }

/* Window Manager */
    {
        char text[256];

        switch(screen_list->wm_type)
        {
        case WM_CARD:
            strcpy(text, "card");
            break;
        case WM_HSPLIT:
            strcpy(text, "hsplit");
            break;
        case WM_VSPLIT:
            strcpy(text, "vsplit");
            break;
        case WM_FULL:
        default:
            strcpy(text, "full");
            break;

        }

        x++;
        cucul_set_color_ansi(cv, CUCUL_LIGHTBLUE, CUCUL_BLUE);
        cucul_printf(cv, x, y, "WM:");
        cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_BLUE);
        cucul_printf(cv, x+4, y, text);
        x+= 4+strlen(text);
    }

/* Help (must be the last one )*/
    {
        char text[256];
        sprintf(text, "Help: ctrl-a-h");
        x = cucul_get_canvas_width(cv) - strlen(text);

        cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_BLUE);
        cucul_printf(cv, x, y, text);
    }


}


void draw_help(cucul_canvas_t *cv, struct screen_list *screen_list, int pty)
{
    int w = 65, h = 20;
    int x = (cucul_get_canvas_width(cv) - w) / 2;
    int y = (cucul_get_canvas_height(cv) - h) / 2;


    cucul_set_color_ansi(cv, CUCUL_BLUE, CUCUL_BLUE);
    cucul_fill_box(cv,
                   x, y,
                   w, h, '#');
    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_BLUE);
    cucul_draw_cp437_box(cv,
                         x, y,
                         w, h);

    x+=2;
    y++;
    cucul_printf(cv,
                 (cucul_get_canvas_width(cv) - strlen(PACKAGE_STRING)) / 2,
                 y-1,
                 PACKAGE_STRING);
    cucul_printf(cv, x, y++, "Copyright (c) 2006-2008 ");
    cucul_printf(cv, x, y++, "              Sam Hocevar <sam@zoy.org>");
    cucul_printf(cv, x, y++, "              Jean-Yves Lamoureux <jylam@lnxscene.org>");
    cucul_printf(cv, x, y++, "");
    cucul_printf(cv, x, y++, "");
    cucul_printf(cv, x, y++, "All shortcuts are in format 'ctrl-a-X' where X is :");

    cucul_printf(cv, x, y++, "n:\t Next window");
    cucul_printf(cv, x, y++, "p:\t Previous window");
    cucul_printf(cv, x, y++, "w:\t Switch window manager");
    cucul_printf(cv, x, y++, "c:\t Create new window");
    cucul_printf(cv, x, y++, "m:\t Thumbnails");
    cucul_printf(cv, x, y++, "k:\t Close window and kill associated process");
    cucul_printf(cv, x, y++, "h:\t This help");
    cucul_printf(cv, x, y++, "");
    cucul_printf(cv, x, y++, "");
    cucul_printf(cv, x, y++, "");
    cucul_printf(cv, x, y++, "See http://libcaca.zoy.org/wiki/neercs for more informations");
}
