#ifndef MARS_MSequence
#define MARS_MSequence

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

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

class MDirIter;

class MSequence : public MParContainer
{
public:
    enum LightCondition_t { kNA, kNoMoon, kTwilight, kMoon, kDay };
    enum FileType_t {
        kRawDat, kRawPed, kRawCal, kRawAll,
        kRootDat, kRootPed, kRootCal, kRootAll,
        kCalibrated, kImages
    };
private:
    TString fFileName;
    TString fDataPath;

    UInt_t fSequence;

    MTime  fStart;

    UInt_t fLastRun;
    UInt_t fNumEvents;

    UInt_t fPeriod;
    MTime  fNight;

    LightCondition_t fLightCondition;

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

    TArrayI fRuns;
    TArrayI fRunsSub;

    TArrayI fCalRuns;
    TArrayI fCalRunsSub;

    TArrayI fPedRuns;
    TArrayI fPedRunsSub;

    TArrayI fDatRuns;
    TArrayI fDatRunsSub;

    TArrayI fExclRuns;
    TArrayI fExclRunsSub;

    Bool_t fMonteCarlo;

    // Helper for interpretation
    void    AddEntry(Int_t run, Int_t file, TArrayI &data, TArrayI &sub) const;
    void    EvalEntry(const TEnv *env, const TString &prefix, const TString &num, TArrayI &data, TArrayI &sub) const;

    void    Split(TString &runs, TArrayI &data, TArrayI &sub, const TEnv *env=0, const TString prefix="") const;
    void    Split(const TEnv &env, const TString &prefix, const char *name, TArrayI &data, TArrayI &sub) const;

    LightCondition_t ReadLightCondition(TEnv &env, const char *prefix) const;

    // Helper for file setup
    TString GetPathName(TString d, FileType_t type) const;
    TString GetFileName(UInt_t num, const TArrayI &arr, const TArrayI &sub, FileType_t type) const;
    UInt_t  SetupRuns(MDirIter &iter, const TArrayI &arr, const TArrayI &file, FileType_t type, const char *path=0) const;

    // Helper for Print()
    TString GetNumSequence(Int_t pos, Int_t n, const TArrayI &f) const;
    TString GetNumSequence(Int_t &pos, const TArrayI &n, const TArrayI &f) const;

    void    PrintRunsClassic(const char *pre, const char *name, const TArrayI &r) const;
    TString PrintRuns(const char *pre, const char *name, const TArrayI &r, const TArrayI &f) const;

    // General helper
    Bool_t  HasSubRuns() const { return fRunsSub.GetSize()!=0 || fDatRunsSub.GetSize()!=0 || fPedRunsSub.GetSize()!=0 || fCalRunsSub.GetSize()!=0 || fExclRunsSub.GetSize()!=0; }
    Bool_t  IsContained(const TArrayI &arr, const TArrayI &sub, UInt_t num, UInt_t file) const;

    // Some helpers to handle the arrays
    static Int_t SortArraySub(Int_t p, Int_t n, Int_t *arr1, Int_t *arr2);
    static void  SortArrays(TArrayI &arr1, TArrayI &arr2);
    static Int_t GetSubArray(Int_t p, Int_t n, Int_t *arr1);

public:
    MSequence() : fSequence((UInt_t)-1), fLastRun((UInt_t)-1),
        fNumEvents((UInt_t)-1), fPeriod((UInt_t)-1), fMonteCarlo(kFALSE) { }
    MSequence(const char *fname, const char *path="", UInt_t id=(UInt_t)-1);
    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), fMonteCarlo(s.fMonteCarlo) { }

    // TObject
    void Print(Option_t *o) const;
    void Print() const { Print(""); } //*MENU*

    const char *GetName() const;
    const char *GetRcName() const { return fName; }

    // Genaral interface
    Bool_t IsValid() const { return fSequence!=(UInt_t)-1; }
    Bool_t IsMonteCarlo() const { return fMonteCarlo; }
    Bool_t IsExcluded(UInt_t run, UInt_t file) const { return IsContained(fExclRuns, fExclRunsSub, run, file); }
    Bool_t IsContained(UInt_t run, UInt_t file) const { return IsContained(fCalRuns, fCalRunsSub, run, file) || IsContained(fPedRuns, fPedRunsSub, run, file) || IsContained(fDatRuns, fDatRunsSub, run, file); }

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

    void AddRun(UInt_t run, char type='*') { AddFile(run, 0, type); }
    void AddRuns(UInt_t run1, UInt_t run2, char type='*') { for (UInt_t i=run1; i<=run2; i++) AddFile(i, 0, type); }
    void AddFile(UInt_t run, UInt_t file, char type='*');
    void AddFiles(UInt_t run, UInt_t f1, UInt_t f2, char type='*') { for (UInt_t i=f1; i<=f2; i++) AddFile(run, i, type); }

    void ExcludeFile(UInt_t num, UInt_t file=0);
    void ExcludeRuns(TString runs);

    void SetMonteCarlo(Bool_t ismc=kTRUE) { fMonteCarlo=ismc; }

    // Getter
    UInt_t GetNumExclRuns() const { return fExclRuns.GetSize(); }

    UInt_t GetSequence() const { return fSequence; }
    UInt_t GetLastRun() const  { return fLastRun; }
    UInt_t GetPeriod() const   { return fPeriod; }
    Bool_t HasMoon() const { return fLightCondition==kMoon; }

    LightCondition_t GetLightCondition() const { return fLightCondition; }

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

    const TString GetExcludedRuns() const;

    // Filesystem interface
    UInt_t SetupPedRuns(MDirIter &iter, const char *path=0, Bool_t raw=kFALSE) const;
    UInt_t SetupDatRuns(MDirIter &iter, const char *path=0, Bool_t raw=kFALSE) const;
    UInt_t SetupAllRuns(MDirIter &iter, const char *path=0, Bool_t raw=kFALSE) const;
    UInt_t SetupCalRuns(MDirIter &iter, const char *path=0, Bool_t raw=kFALSE) const;
    UInt_t SetupDatRuns(MDirIter &iter, FileType_t type, const char *path=0) const;

    // Filesystem getter
    const TString &GetFileName() const { return fFileName; }
    const TString &GetDataPath() const { return fDataPath; }

    const TString GetStandardPath() const { return fMonteCarlo?"/magic/montecarlo/":"/magic/data/"; }

    static Bool_t InflatePath(TString &seq, Bool_t ismc=kFALSE);

    ClassDef(MSequence, 5) // Describes a sequences, reads and writes sequence files
};

#endif
