/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

    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 LICENSE and COPYING files for more details.
*/

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "clutotreescene.h"
#include "dendrogramscene.h"
#include <map>

class ClutoTreeScene;

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::MainWindow *ui;

    /**
      Pointers to QGraphicScenes
      */
    ClutoTreeScene *scene;
    DendrogramScene *dScene;

    std::vector<std::string> labels;

    /**
      Map representing parent and vector of its children
      */
    std::map<int, std::vector<size_t> > children;

    /**
      Similarities of subsequent nodes
      */
    std::vector<double> similarities;

    /**
      Root of current tree
      */
    Node * root;

    /**
      Number of levels (depth) in current tree
      */
    size_t maxlevel;

    /**
      Number of all nodes (including leaves and parents) in current tree
      */
    size_t nodes;

    /**
      Read the file including tree in CLUTO format
      */
    void ReadTreeFile();

    /**
      Read file with external similarities
      Number of lines must be equal to number of *nodes* in the tree
      */
    void ReadSimFile();

    /**
      Read file with labels
      Number of lines must be equal to number of *leaves* in the tree
      */
    void ReadLabelFile();

    /**
      Iterate through the children map and build logical representation of the tree
      (using Insert function)
      */
    void BuildTree();

    /**
      Insert nodes into the tree (recursive)
      Returns number of levels ahead from current node
      */
    size_t Insert(Node * root, size_t level);

    /**
      Search min and max similarities in subtrees (recursive)
      */
    void Traverse(Node * node, double & min, double & max);

    QLabel *treeFile, *labelsFile;

    /**
      Current scale of QGraphicsView magnification (default 1.0)
      */
    double currentScale;

private slots:
    void on_pushButton_2_clicked();
    void on_pushButton_clicked();
    void on_pushButton_4_clicked();
    void on_spinBox_valueChanged(int );
    void on_spinBox_2_valueChanged(int );
    void on_spinBox_3_valueChanged(int );
    void on_spinBox_4_editingFinished();
    void on_pushButton_3_clicked();    
    void on_checkBox_stateChanged(int );
    void on_comboBox_currentIndexChanged(int index);
    void on_comboBox_2_currentIndexChanged(int index);

    /**
      Redraw scene if tree is available
      */
    void RedrawScene();

    /**
      Center scene on required point
      */
    void CenterScene(int x, int y);

    /**
      Traverse subtree with *node as a root (recursive) and read labels from the leaves
      */
    void FillLabelList(Node * node);

    /**
      Find min and max similarities in subtree with *node as a root
      Use recursive method - Traverse
      */
    void FindMinMaxSimilarities(Node * node);
};

#endif // MAINWINDOW_H
