/*
 * creationmanager.h - author: Andrew Craig
 *
 * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.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 (version 2 of the License)
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
#ifndef PS_CHAR_CREATION_MANAGER_H
#define PS_CHAR_CREATION_MANAGER_H

#include "msgmanager.h"
#include "net/charmessages.h"

/* Server manager for character creation.
 * This guy loads all the data that may be needed to be sent to the client
 * for the character creation.  It loads data from these tables:
 *   race_info
 *   character_creation
 *
 */
class psCharCreationManager : MessageManager
{

private:
    // Structure to hold the initial CP race values.
    struct RaceCP
    {
        int id;
        int value;
    };
    
    ////////////////////////////////////////////////////
    // Structure for cached character creation choices
    ////////////////////////////////////////////////////
public:    
    struct CreationChoice
    {
        int id;                     
        csString name;              
        csString description;       
        int choiceArea;
        int cpCost;
        csString eventScript;
    };
    ////////////////////////////////////////////////////
private:    
    //////////////////////////////////////////////////////
    // Structure for cached character creation life event
    //////////////////////////////////////////////////////    
    class LifeEventChoiceServer : public LifeEventChoice
    {
    public:
        csString eventScript;       
    };        
    //////////////////////////////////////////////////////    
    
    
public:
    
    psCharCreationManager();
    virtual ~psCharCreationManager();
    
    /** Caches the data from the database needed for character creation.
     */
    bool Initialize();
          
    /** Handles incomming net messages.
     * Have to add what messages it is subscribed to.
     */    
    virtual void HandleMessage(MsgEntry *pMsg,Client *client);
    
    LifeEventChoiceServer* FindLifeEvent( int id );
    CreationChoice* FindChoice( int id );
    
    bool Validate( psCharUploadMessage& mesg, csString& errorMsg );
    
protected:
    
    /// Cache in the data about the race CP starting values.
    bool LoadCPValues();
    
    /// Caches in creation choices from the database.
    bool LoadCreationChoices();
    
    bool LoadLifeEvents();
    
    RaceCP* raceCPValues;
    int raceCPValuesLength;    // length of the raceCPValues array
    
    /// A list of all the for the parent screen.
    csPDelArray<CreationChoice> parentData;
    csPDelArray<CreationChoice>  childhoodData;
    csPDelArray<LifeEventChoiceServer> lifeEvents;
    
    /** Takes a string name of a choice and converts it to it's enum value.
     * This is useful to have string names in the database ( ie human readable ) but
     * then use them as ints in the application ( ie easier to use ).
     *
     * @param area The name of the area to convert.
     * @return The int value of that area. Will be one of the defined enums of areas.
     */      
    int ConvertAreaToInt( const char* area );
    
    /** Handle and incomming message from a client about parents.
     * This handles a request from the client to send it all the data about choice 
     * areas in the parents screen. 
     */
    void HandleParents( MsgEntry* me,Client *client );
    
    void HandleChildhood( MsgEntry* me,Client *client );
    
    void HandleLifeEvents( MsgEntry* me,Client *client );    

    void HandleTraits( MsgEntry* me,Client *client );    
    
    /** Handles a check on a name. 
      * This will check the name against the migration and the current 
      * characters table.  It will send back a message to the client to
      * notify them of the name status. If rejected it will include a 
      * message why.
      */
    void HandleName( MsgEntry* me ,Client *client);
  
    /** Handles the deletion of a character from the char pick screen.
      */
    bool HandleCharDelete( MsgEntry* me, Client* client );
    
    int CalculateCPChoices( csArray<uint32_t>& choices, int fatherMod, int motherMod );
    int CalculateCPLife( csArray<uint32_t>& events );  
           
};



#endif
