#ifndef ENGINEPREVIEW_H
#define ENGINEPREVIEW_H

#include <QGLWidget>
#include "engine_structs.h"
#include "engineinterface.h"
#include "structures.h"
#include "magic3d.h"
#include <map>
#include <vector>
#include <QTimer>
#include "me_time.h"

struct ObjectInfo
{
    M3D::Coords position;
    M3D::Scale scale;
    M3D::Rotation rotation;
};
struct BaseObject
{
    M3D::ModelInterface base_model;
    std::string path;
    std::string name;
    std::map<M3D::ModelInterface, ObjectInfo> model_list;
};
#pragma pack(push, 1)
struct CameraPath
{
    M3D::Coords position;
    M3D::Rotation rotation;
    bool is_loop_start;
    float length;
};
#pragma pack(pop)

namespace Ui {
    class EnginePreview;
}

class EnginePreview : public QGLWidget {
    Q_OBJECT
public:
    EnginePreview(QWidget *parent = 0);
    ~EnginePreview();
    void set_landscape(char *model_path);
    void set_water(char *model_path);
    void load_base_object(const char *model_path, const char *model_name);
    bool check_base_object(const char *model_name);
    int add_object(const char *base_model, float x, float y, float z, float scale);
    int find_nearest_object(const char *base_model, float x, float z);
    bool remove_object(const char *base_model, int oid);
    void clear_object_list();
    std::vector<M3D::Coords> get_object_list();
    ObjectInfo* get_object_info(const char *base, int oid);
    void save(const std::string &fname);
    void load(const std::string &fname);
    int add_cam_path_point(float x, float y, float z, float x_ang, float y_ang, float z_ang, bool is_loop_start, float length);
    CameraPath* get_cam_path_point(int id);

protected:
    void changeEvent(QEvent *e);
    void initializeGL ();
    void resizeGL ( int width, int height );
    void glDraw ();

private:
    Ui::EnginePreview *m_ui;

    M3D::EngineInterface *engine;
    M3D::EngineLimits limits;
    M3D::ModelInterface landscape_model;
    M3D::ModelInterface water_model;
    QTimer timer;

public:
    M3D::Scale base_scale;

    M3D::Coords camera_position;
    M3D::CameraParams camera_params;
    M3D::Rotation camera_rotation;
    M3D::CameraInterface camera;

    M3D::Coords light_position;
    M3D::LightInterface light;

    M3D::Coords water_position;

    std::map<std::string, BaseObject> models;
    std::vector<CameraPath> camera_path;

    int frame_delay;
    int frame_no;
    bool isAnimationMode;
    int current_point;
    int loop_point;
    Time time;
    Time animation_timer;

};

#endif // ENGINEPREVIEW_H
