/***************************************************************************
                          playerview.cpp  -  description
                             -------------------
    begin                : Fri Sep 19 10:27:05 CET 2006
    copyright            : (C) 2006 by Harald Krippel
    email                : harald@the-develop.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

 // Qt includes
#include <qsettings.h>

// application specific includes
#include "playerview.h"
#include "glcontrol.h"


#include <iostream>

PlayerView::PlayerView(QObject *parent) :QObject(parent)
{
    gl = NULL;
    tmpItem = NULL;

    folders = new QListView( );
    folders->setSorting( -1 );
    folders->addColumn( "Game" );
    folders->addColumn( "Type" );
    folders->addColumn( "ID" );
    
    folders->setRootIsDecorated( TRUE );
}



void PlayerView::newWirebox(FolderListItem * i)
{
    if((g_gl))   // no GLWindow
    {
      i->gl=g_gl;
      g_gl->initFolderListItem(i,g_prjpath);
      g_gl->setFocusScene(i);
    }
    else
    {
    
    }
}

void PlayerView::initListView(QDomDocument *domdoc, const QString &path)
{
  QDomNode node;
  int a;

  if(g_gl != NULL)
  {
    if ( g_gl->scene != NULL )
    {
      g_gl->puDelete ();
    }
  }


  folders->clear();
  for(a=0; a < MAX_LIST_ITEMS; a++)
  {
    g_ListItem[a] = NULL;
  }
  g_nextid=0;

  mypath=path;
  
  QDomElement docElem = domdoc->documentElement();

      // create the tree view out of the DOM
    node = docElem.firstChild();
    while ( !node.isNull() )
    {
	    if ( node.isElement() && node.nodeName() == "body" )
      {
	      QDomElement body = node.toElement();
	      buildTree( 0, body );
	      break;
	    }
	    node = node.nextSibling();
    }
}

void PlayerView::buildTree( FolderListItem *parentItem, const QDomElement &parentElement )
{
    FolderListItem *thisItem = 0;
    FolderListItem *nextItem = 0;
    unsigned int a=0;
             
    QDomNode node = parentElement.firstChild();
    while ( !node.isNull() )
    {
    	if ( node.isElement() && node.nodeName()== "outline" )
      {

  	if ( parentItem == 0 )
        {
	      	thisItem = new FolderListItem( folders, thisItem );
        }
        else
        {
		      thisItem = new FolderListItem( parentItem, thisItem );
        }
	      thisItem->setText( 0, node.toElement().attribute( "text" ) );
	      thisItem->setText( 1, node.toElement().attribute( "type" ) );
	      thisItem->setText( 2, node.toElement().attribute( "tmpid" ) );

        if(g_nextid < node.toElement().attribute( "tmpid" ).toInt() )
        {
          g_nextid = node.toElement().attribute( "tmpid" ).toInt();
        }
        g_ListItem[g_nextitem] = thisItem;
        g_nextitem++;

        for(a=0; a< node.attributes().count();a++)
        {
          if(strstr(node.attributes().item(a).nodeName(),"text")==NULL &&
           strstr(node.attributes().item(a).nodeName(),"type")==NULL)
          {
            nextItem = new FolderListItem( thisItem, nextItem );
	          nextItem->setText( 0, node.attributes().item(a).nodeName() );
	          nextItem->setText( 1, node.attributes().item(a).nodeValue() );
          }
        }                            
	        // recursive build of the tree
          buildTree( thisItem, node.toElement() );
	    }
	    node = node.nextSibling();
    }
    g_nextid++;
}


void PlayerView::getListView(QDomDocument *domdoc)
{
    QDomElement root = domdoc->createElement( "opml" );
    domdoc->appendChild( root );

    root.setAttribute( "version", "1.0" );

    // Header
    
    QDomElement head = domdoc->createElement( "head" );
    root.appendChild( head );

    QDomElement title = domdoc->createElement( "title" );
    head.appendChild( title );

    QDomText t = domdoc->createTextNode( "Nonamed.xml" );
    title.appendChild( t );

    // Body
    
    QDomElement body = domdoc->createElement( "body" );
    root.appendChild( body );

    buildDomTree(&body,(FolderListItem *)(folders->firstChild()),domdoc);
}

void PlayerView::buildDomTree( QDomElement *parentElement, FolderListItem *parentItem,QDomDocument *domdoc )
{
    QListViewItem *ichild = parentItem;
    QDomElement outline;

    while(ichild != NULL)
    {
      
        if(ichild->text(1) == "OPTION" ||
         ichild->text(1) == "LAMP" ||
         ichild->text(1) == "CAMERA" ||
         ichild->text(1) == "OBJECT" ||
         ichild->text(1) == "BARRIER" ||
         ichild->text(1) == "BODY" ||
         ichild->text(1) == "PARTSYS" ||
         ichild->text(1) == "WAVESYS" ||
         ichild->text(1) == "FIRE" ||
         ichild->text(1) == "FOG" ||
         ichild->text(1) == "PLAYER" ||
         ichild->text(1) == "SKY" ||
         ichild->text(1) == "TERRAIN" ||
         ichild->text(1) == "SHADER" ||
         ichild->text(1) == "CURSOR" ||
         ichild->text(1) == "SPL" ||
         ichild->text(1) == "SOUND" ||
         ichild->text(1) == "GUI-TXT" ||
         ichild->text(1) == "GUI-INPUT" ||
         ichild->text(1) == "GUI-BUTTON" ||
         ichild->text(1) == "GUI-OSBUTTON" ||
         ichild->text(1) == "GLOBAL" ||
         ichild->text(1) == "NETWORK" ||
         ichild->text(1) == "INTRO" ||
         ichild->text(1) == "LEVEL" ||
         ichild->text(1) == "SCENE" ||
         ichild->text(1) == "HISCORE" ||
         ichild->text(1) == "END")
        {
          outline = domdoc->createElement( "outline" );

          QListViewItem *achild = ichild->firstChild();
          while(achild != NULL)
          {

            if(!(achild->text(1) == "OPTION" ||
            achild->text(1) == "LAMP" ||
            achild->text(1) == "CAMERA" ||
            achild->text(1) == "OBJECT" ||
            achild->text(1) == "BARRIER" ||
            achild->text(1) == "BODY" ||
            achild->text(1) == "PARTSYS" ||
            achild->text(1) == "WAVESYS" ||
            achild->text(1) == "FIRE" ||
            achild->text(1) == "FOG" ||
            achild->text(1) == "PLAYER" ||
            achild->text(1) == "SKY" ||
            achild->text(1) == "TERRAIN" ||
            achild->text(1) == "SHADER" ||
            achild->text(1) == "CURSOR" ||
            achild->text(1) == "SPL" ||
            achild->text(1) == "SOUND" ||
            achild->text(1) == "GUI-TXT" ||
            achild->text(1) == "GUI-INPUT" ||
            achild->text(1) == "GUI-BUTTON" ||
            achild->text(1) == "GUI-OSBUTTON" ||
            achild->text(1) == "GLOBAL" ||
            achild->text(1) == "NETWORK" ||
            achild->text(1) == "INTRO" ||
            achild->text(1) == "LEVEL" ||
            achild->text(1) == "SCENE" ||
            achild->text(1) == "HISCORE" ||
            achild->text(1) == "END"))
            {

              outline.setAttribute( achild->text(0), achild->text(1) );

            }
            achild=achild->nextSibling();
          }
          outline.setAttribute( "tmpid", ichild->text(2) );
          outline.setAttribute( "type", ichild->text(1) );
          outline.setAttribute( "text", ichild->text(0) );
          parentElement->appendChild( outline );
        }
        qWarning( ichild->text(0) + " OK !" );
        buildDomTree(&outline,(FolderListItem *)(ichild->firstChild()),domdoc);
        ichild=ichild->nextSibling();
    }
}

PlayerView::~PlayerView()
{
}

int PlayerView::getScene()
{
  int a;
  int mid; 
  for(a=0;a<g_nextitem;a++)
  {
  	if(g_ListItem[a] != NULL)
        {
           if(g_ListItem[a]->text(1) == "GLOBAL")
           {
  		qWarning( "find GLOBAL");
          	mid = g_ListItem[a]->GetValue("sscene").toInt();
                if(mid < MAX_LIST_ITEMS && g_ListItem[mid] != NULL)
                {
                	return mid;        
                }	
           }
        }
  }
  qWarning( "cannt find GLOBAL -> sscene");
  qWarning( "exit");
  exit ( 0 ) ; // error
  return 4; // Scene1
}

FolderListItem *PlayerView::getStart()
{
  int a;
  int mid=0;
   
  mid =  getScene();
  
  for(a=0;a<g_nextitem;a++)
  {
  	if(g_ListItem[a] != NULL)
        {
           if(g_ListItem[a]->text(1) == "SCENE" && g_ListItem[a]->GetValue("tmpid").toInt() == mid)
           {
                	qWarning( "getStart %i", mid );
                	return g_ListItem[a];        
           }
        }
  }
  qWarning( "cant find Start %i, return NULL",mid);
  qWarning( "exit");
  exit ( 0 ) ; // error
  return NULL; // Scene1
}
