/*

                                   _JNJ`                        
                               .JNMH`                           
                              JMMF`       `;.                   
                            .NMM)          `MN.                 
                            MMM)            (MML                
                           (MMM`             MMML               
                     M  I  D  N  I  G  H  T    C  o  D  E       
                           (NMMF             MHNH               
                            NMML            .MMM                
                             NMML          .NMH                 
                              4MMNL       .#F                   
                               `4HNNL_   `                      
                                   `"""`                        

                          Copyright (C) 2004-2006
       "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, and mirrored at the Midnight Code web
    site; as at version 2 of the License only.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
  
    You should have received a copy of the GNU General Public License
    (version 2) along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA, or see http://midnightcode.org/gplv2.txt

*/

/* midnightcode-status.c :: Midnight Code status display library	*/


#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "midnightcode-colour.h"
#include "midnightcode-status.h"


/* TODO: need new function for one-hit text+status message combo */

int
midnightcode_status_text(int type, int style, const char *fmt, ...) {
  va_list args;
  char    buf[81];
  char    pad[81];
  int     padding;

  memset(buf, 0, 81);

  va_start(args, fmt);
  vsnprintf(buf, 80, fmt, args);
  va_end(args);

  switch(type) {

    case MIDNIGHTCODE_STATUS_TYPE_H1:
      memset(pad, 0, 81);
      padding = 76 - strlen(buf);
      if(padding > 0) {
        memset(pad, '_', padding);
      } else {
        snprintf(buf, 76, "%s", buf);
      }
      switch(style) {
        case MIDNIGHTCODE_STATUS_STYLE_SOFT:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_DK_GREY
                          "\n\n%s [%s]\n\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, pad, buf);
          fflush(stdout);
          break;

        case MIDNIGHTCODE_STATUS_STYLE_NORMAL:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_NORMAL
                          "\n\n%s [%s]\n\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, pad, buf);
          fflush(stdout);
          break;

        case MIDNIGHTCODE_STATUS_STYLE_STRONG:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_WHITE
                          "\n\n%s [%s]\n\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, pad, buf);
          fflush(stdout);
          break;

        default:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_YELLOW
                          "\n\n%s [%s]\n\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, pad, buf);
          fflush(stdout);
          type = 0;
          break;

      }
      break;

    case MIDNIGHTCODE_STATUS_TYPE_H2:
      switch(style) {
        case MIDNIGHTCODE_STATUS_STYLE_SOFT:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_DK_GREY
                          "\n  %-65.65s\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        case MIDNIGHTCODE_STATUS_STYLE_NORMAL:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_NORMAL
                          "\n  %-65.65s\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        case MIDNIGHTCODE_STATUS_STYLE_STRONG:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_WHITE
                          "\n  %-65.65s\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        default:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_YELLOW
                          "\n  %-65.65s\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          type = 0;
          break;

      }
      break;

    case MIDNIGHTCODE_STATUS_TYPE_TEXT:
      switch(style) {
        case MIDNIGHTCODE_STATUS_STYLE_SOFT:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_DK_GREY
                          "    %-60.60s  "
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        case MIDNIGHTCODE_STATUS_STYLE_NORMAL:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_NORMAL
                          "    %-60.60s  "
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        case MIDNIGHTCODE_STATUS_STYLE_STRONG:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_WHITE
                          "    %-60.60s  "
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        default:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_YELLOW
                          "    %-60.60s  "
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          type = 0;
          break;

      }
      break;

    case MIDNIGHTCODE_STATUS_TYPE_BLURB:
      switch(style) {
        case MIDNIGHTCODE_STATUS_STYLE_SOFT:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_DK_GREY
                          "          %-50.50s\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        case MIDNIGHTCODE_STATUS_STYLE_NORMAL:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_NORMAL
                          "          %-50.50s\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        case MIDNIGHTCODE_STATUS_STYLE_STRONG:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_WHITE
                          "          %-50.50s\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          break;

        default:
          fprintf(stdout, MIDNIGHTCODE_COLOUR_YELLOW
                          "          %-50.50s\n"
                          MIDNIGHTCODE_COLOUR_NORMAL, buf);
          fflush(stdout);
          type = 0;
          break;

      }
      break;

    default:
      break;

  }

  return(type);
}


int
midnightcode_status_flag(int status) {

  switch(status) {
    case MIDNIGHTCODE_STATUS_FLAG_OKAY:
      fprintf(stdout, MIDNIGHTCODE_COLOUR_NORMAL "[ "
                      MIDNIGHTCODE_COLOUR_BT_GREEN " OK "
                      MIDNIGHTCODE_COLOUR_NORMAL " ]\n");
      fflush(stdout);
      break;

    case MIDNIGHTCODE_STATUS_FLAG_WARN:
      fprintf(stdout, MIDNIGHTCODE_COLOUR_NORMAL "[ "
                      MIDNIGHTCODE_COLOUR_BT_YELLOW "WARN"
                      MIDNIGHTCODE_COLOUR_NORMAL " ]\n");
      fflush(stdout);
      break;

    case MIDNIGHTCODE_STATUS_FLAG_FAIL:
      fprintf(stdout, MIDNIGHTCODE_COLOUR_NORMAL "[ "
                      MIDNIGHTCODE_COLOUR_BT_RED "FAIL"
                      MIDNIGHTCODE_COLOUR_NORMAL " ]\n");
      fflush(stdout);
      break;

    default:
      fprintf(stdout, MIDNIGHTCODE_COLOUR_NORMAL "[ "
                      MIDNIGHTCODE_COLOUR_YELLOW "UNKNOWN"
                      MIDNIGHTCODE_COLOUR_NORMAL " ]\n");
      fflush(stdout);
      status = 0;
      break;
  }

  return(status);
}


