#ifndef MARS_MSequence
#define MARS_MSequence

#ifndef ROOT_TArrayI
#include <TArrayI.h>
#endif

#ifndef ROOT_TExMap
#include <TExMap.h>
#endif

#ifndef MARS_MTime
#include "MTime.h"
#endif

class MDirIter;

class MSequence : public MParContainer
{
private:
    UInt_t fSequence;

    MTime  fStart;

    UInt_t fLastRun;
    UInt_t fNumEvents;

    UInt_t fPeriod;
    MTime  fNight;

    TString fProject;
    TString fSource;
    TString fTriggerTable;
    TString fHvSettings;

    TArrayI fRuns;
    TArrayI fCalRuns;
    TArrayI fPedRuns;
    TArrayI fDatRuns;

    //TExMap fFileNames;

    void Split(TString &runs, TArrayI &data) const;
    void GetFileNames(TEnv &env, const TArrayI &arr);

    const char *GetFileName(UInt_t num);

    UInt_t SetupRuns(MDirIter &iter, const TArrayI &arr, const char *path, char *id, Bool_t raw=kFALSE) const;
    UInt_t AddRuns(UInt_t first, UInt_t last, TArrayI *runs);

public:
    MSequence() : fSequence((UInt_t)-1), fLastRun((UInt_t)-1),
        fNumEvents((UInt_t)-1), fPeriod((UInt_t)-1) { }
    MSequence(const char *fname);
    MSequence(const MSequence &s) : fSequence(s.fSequence), fStart(s.fStart),
        fLastRun(s.fLastRun), fNumEvents(s.fNumEvents), fPeriod(s.fPeriod),
        fNight(s.fNight), fProject(s.fProject), fSource(s.fSource),
        fTriggerTable(s.fTriggerTable), fHvSettings(s.fHvSettings),
        fRuns(s.fRuns), fCalRuns(s.fCalRuns), fPedRuns(s.fPedRuns),
        fDatRuns(s.fDatRuns) { }
    ~MSequence();

    // TObject
    void Print(Option_t *o="") const;

    // Genaral interface
    Bool_t IsValid() const { return fSequence!=(UInt_t)-1; }

    UInt_t SetupPedRuns(MDirIter &iter, const char *path=0, char *id="P", Bool_t raw=kFALSE) const;
    UInt_t SetupDatRuns(MDirIter &iter, const char *path=0, char *id="D", Bool_t raw=kFALSE) const;
    UInt_t SetupAllRuns(MDirIter &iter, const char *path=0, char *id="?", Bool_t raw=kFALSE) const;
    UInt_t SetupCalRuns(MDirIter &iter, const char *path=0, char *id="C", Bool_t raw=kFALSE) const;

    // Getter
    UInt_t GetNumAllRuns() const { return fRuns.GetSize(); }
    UInt_t GetNumDatRuns() const { return fDatRuns.GetSize(); }
    UInt_t GetNumPedRuns() const { return fPedRuns.GetSize(); }
    UInt_t GetNumCalRuns() const { return fCalRuns.GetSize(); }

    UInt_t GetSequence() const { return fSequence; }
    UInt_t GetLastRun() const  { return fLastRun; }
    UInt_t GetPeriod() const   { return fPeriod; }

    const UInt_t GetFirstRun() const { return fRuns[0]; }
    const UInt_t GetFirstCalRun() const { return fCalRuns[0]; }
    const UInt_t GetLastCalRun() const { return fCalRuns[fCalRuns.GetSize()-1]; }
    const TArrayI &GetPedRuns() const { return fPedRuns; } 
    const TArrayI &GetCalRuns() const { return fCalRuns; } 
    const TArrayI &GetDatRuns() const { return fDatRuns; } 

    const MTime &GetStart() const { return fStart; }
    const MTime &GetNight() const { return fNight; }
    const TString &GetSource() const { return fSource; } 

    const TString GetStandardPath(Bool_t raw) const;

    // Setter
    void SetNight(const char*night);

    UInt_t AddRuns(UInt_t first, UInt_t last)    { return MSequence::AddRuns(first, last, 0); }
    UInt_t AddCalRuns(UInt_t first, UInt_t last) { return MSequence::AddRuns(first, last, &fCalRuns); }
    UInt_t AddPedRuns(UInt_t first, UInt_t last) { return MSequence::AddRuns(first, last, &fPedRuns); }
    UInt_t AddDatRuns(UInt_t first, UInt_t last) { return MSequence::AddRuns(first, last, &fDatRuns); }

    UInt_t AddRuns(UInt_t num)    { return AddRuns(num, num); }
    UInt_t AddCalRuns(UInt_t num) { return AddCalRuns(num, num); }
    UInt_t AddPedRuns(UInt_t num) { return AddPedRuns(num, num); }
    UInt_t AddDatRuns(UInt_t num) { return AddDatRuns(num, num); }

    ClassDef(MSequence, 0)
};

#endif
