/*

                                   _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-2007
       "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>

    Midnight Code is a registered trademark of Ian Latter.

    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-ai.c :: Midnight Code Artificial Intelligence library	*/


#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <ctype.h>
#include <arpa/inet.h>

#include "preferences.h"
#include "midnightcode-list.h"
#include "midnightcode-html.h"



int
midnightcode_ai_learn(char * id, void *object, int score
                       struct midnightcode_ai_head_t * strategies) {
  struct midnightcode_ai_element_t * strategy;

  if(!strategies)
    return -1;

  /*
     [1] Initialize string table;
     [2] [.c.] <- empty;
     [3] K <- next character in charstream;
     [4] Is [.c.]K in string table?
      (yes: [.c.] <- [.c.]K;
            go to [3];
      )
      (no: add [.c.]K to the string table;
           output the code for [.c.] to the codestream;
           [.c.] <- K;
           go to [3];
      )
  */

  midnightcode_list_for_each(strategy, strategies) {
    if(!strategy->id)
      continue;

    if(strcmp(strategy->id, id) == 0) {


  }


  if(score == MIDNIGHTCODE_AI_SCORE_FAILURE)
    doc_element = malloc(sizeof(struct midnightcode_html_document_element_t));
    memset(doc_element, 0,
                         sizeof(struct midnightcode_html_document_element_t));
    doc_element->tag = local_tag;
    midnightcode_list_append(document, doc_element);

    local_tag_element = malloc(




}



  struct midnightcode_html_document_element_t * doc_element;
  struct midnightcode_html_tag_element_t * local_tag_element;
  struct midnightcode_html_tag_head_t * local_tag;
  midnightcode_list_init(html_tag_struct_head,
                         midnightcode_html_tag_element_t);

  if(!tag)
    return -1;
  if(!*tag_element && equal_state)
    return -1;
  if(!document && !tag_state)
    return -1;

  str_len = (int)(parse_src - store_src);

  if(!tag_state) {
    local_tag = malloc(sizeof(struct html_tag_struct_head));
    memset(local_tag, 0, sizeof(struct html_tag_struct_head));

    doc_element = malloc(sizeof(struct midnightcode_html_document_element_t));
    memset(doc_element, 0,
                         sizeof(struct midnightcode_html_document_element_t));
    doc_element->tag = local_tag;
    midnightcode_list_append(document, doc_element);

    local_tag_element = malloc(
                         sizeof(struct midnightcode_html_tag_element_t));
    memset(local_tag_element, 0,
                         sizeof(struct midnightcode_html_tag_element_t));
    midnightcode_list_append(local_tag, local_tag_element);

    local_tag_element->option = malloc(
                         strlen(MIDNIGHTCODE_HTML_TEXT_TAG) + 1);
    memset(local_tag_element->option, 0,
                         strlen(MIDNIGHTCODE_HTML_TEXT_TAG) + 1);
    strncpy(local_tag_element->option,
                         MIDNIGHTCODE_HTML_TEXT_TAG,
                         strlen(MIDNIGHTCODE_HTML_TEXT_TAG));

    local_tag_element->value = malloc(str_len + 1);
    memset(local_tag_element->value, 0, str_len + 1);
    strncpy(local_tag_element->value, store_src, str_len);

  } else {
    if(str_len > 0) {
      if(!equal_state) {
        *tag_element = malloc(
                         sizeof(struct midnightcode_html_tag_element_t));
        memset(*tag_element, 0,
                         sizeof(struct midnightcode_html_tag_element_t));
        (*tag_element)->option = malloc(str_len + 1);
        memset((*tag_element)->option, 0, str_len + 1);
        strncpy((*tag_element)->option, store_src, str_len);
        midnightcode_list_append(tag, (*tag_element));

      } else {
        (*tag_element)->value = malloc(str_len + 1);
        memset((*tag_element)->value, 0, str_len + 1);
        strncpy((*tag_element)->value, store_src, str_len);

      }
    }
  }

  return str_len;
}


int
midnightcode_html_parse(char *parse_src, int parse_len,
                        struct midnightcode_html_document_head_t * document) {
  char *text_dst, *store_src;
  struct midnightcode_html_document_element_t * doc_element;
  struct midnightcode_html_tag_element_t * tag_element;
  struct midnightcode_html_tag_head_t * tag;
  int tag_state;
  int equal_state;
  int quote_state;
  int quote_type;
  int str_len;
  midnightcode_list_init(html_tag_struct_head,
                         midnightcode_html_tag_element_t);

  if(!document || !parse_src || parse_len < 1)
    return -1;

  store_src = parse_src;
  quote_type = 0;
  quote_state = 0;
  equal_state = 0;
  tag_state = 0;
  str_len = 0;
  text_dst = NULL;
  tag_element = NULL;
  tag = NULL;

  while(*parse_src != '\0' && parse_len > 0) {

    if(*parse_src == '<' && !quote_state) {
      if(tag_element) {
        str_len = midnightcode_html_tagbit(parse_src, store_src,
                  tag_state, equal_state, &tag_element, tag, document);
        if(str_len < 0)
          return -1;
      }
      tag = malloc(sizeof(struct html_tag_struct_head));
      memset(tag, 0, sizeof(struct html_tag_struct_head));

      doc_element = malloc(
                      sizeof(struct midnightcode_html_document_element_t));
      memset(doc_element, 0,
                      sizeof(struct midnightcode_html_document_element_t));
      doc_element->tag = tag;
      midnightcode_list_append(document, doc_element);
      store_src = parse_src + 1;

      tag_state = 1;
      equal_state = 0;
      quote_state = 0;

    } else if(*parse_src == '>' && tag_state && !quote_state) {
      str_len = midnightcode_html_tagbit(parse_src, store_src,
                  tag_state, equal_state, &tag_element, tag, document);
      if(str_len < 0)
        return -1;
      store_src = parse_src + 1;

      tag_state = 0;
      equal_state = 0;
      quote_state = 0;

    } else if(*parse_src == '\'' && tag_state &&
              (!quote_state || (quote_state && quote_type == 2))) {
      str_len = midnightcode_html_tagbit(parse_src, store_src,
                  tag_state, equal_state, &tag_element, tag, document);
      if(str_len < 0)
        return -1;
      store_src = parse_src + 1;
      quote_state = !quote_state;
      quote_type = 0;
      if(quote_state)
        quote_type = 2;

    } else if(*parse_src == '"' && tag_state &&
              (!quote_state || (quote_state && quote_type == 1))) {
      str_len = midnightcode_html_tagbit(parse_src, store_src,
                  tag_state, equal_state, &tag_element, tag, document);
      if(str_len < 0)
        return -1;
      store_src = parse_src + 1;
      quote_state = !quote_state;
      quote_type = 0;
      if(quote_state)
        quote_type = 1;

    } else if(tag_state && !quote_state &&
             (*parse_src == ' ' || *parse_src == '\t' ||
              *parse_src == '\r' || *parse_src == '\n' ||
              *parse_src < 32 || *parse_src > 126)) {
      str_len = midnightcode_html_tagbit(parse_src, store_src,
                  tag_state, equal_state, &tag_element, tag, document);
      if(str_len < 0)
        return -1;
      if(equal_state)
        equal_state = 0;
      store_src = parse_src + 1;
      
    } else if(*parse_src == '=' && tag_state && !quote_state) {
      /* TODO: Consider the impact of equal_state = 1 passing thru here */
      str_len = midnightcode_html_tagbit(parse_src, store_src,
                  tag_state, equal_state, &tag_element, tag, document);
      if(str_len < 0)
        return -1;
      store_src = parse_src + 1;
      equal_state = 1;

    } else if(*(parse_src + 1) == '\0') {
      str_len = midnightcode_html_tagbit(parse_src, store_src,
                  tag_state, equal_state, &tag_element, tag, document);
      if(str_len < 0)
        return -1;
      store_src = parse_src + 1;

    }
    parse_src++;
    parse_len--;

  }

  return parse_len;
}

