#pragma once

/* $Header: https://svn.mpia.de/gulli/TwiceAsNice/branch/unstable/ln/ltcs/test/LbtoIIFSimulator/LbtoIIFInterfaceI.h 14502 2017-07-17 08:46:15Z mathar $
*/

#include <Ice/Ice.h>
#include <IceUtil/IceUtil.h>
#include <Nice/Map.h>
#include <LbtoIIFInterface.h>

namespace lbto
{
    class IIFServerI : virtual public lbto::IIFServer
    {
        public:

            /** Coordinates of the observatory on the Earth crust.
            */
            const Nice::GeoLocation geosite ;

            IIFServerI();

            ~IIFServerI() ;

            virtual result Authorize(const Ice::Current&);

            virtual result StepFocus(double relPos, const std::string& ope,
                                     const std::string& side,
                                     const Ice::Current&);

            virtual result MoveFocus(double absPos, const std::string& ope,
                                     const std::string& side,
                                     const Ice::Current&);

            virtual result OffsetPointing(double rotangle, double offx,
                                          double offy,
                                          const std::string& coordsys,
                                          const std::string& ope,
                                          const std::string& np,
                                          const std::string& movetype,
                                          const std::string& side,
                                          const Ice::Current&);
            virtual result OffsetPointing2(double rotangle, double offx,
                                          double offy,
                                          const std::string& coordsys,
                                          const std::string& movetype,
                                          const std::string& side,
                                          const Ice::Current&);

            virtual result OffsetGuiding(double rotangle, double offx,
                                         double offy,
                                         const std::string& coordsys,
                                         const std::string& side,
                                         const Ice::Current&);

            virtual result UpdatePointingReference(
                    const std::string& offset_coord, const std::string& side,
                    const Ice::Current&);

            virtual result TipTilt(double xrot, double yrot,
                                   const std::string& ope,
                                   const std::string& side,
                                   const Ice::Current&);

            virtual result SetPMTerm(const std::string& name, double value,
                                     const std::string& side,
                                     const Ice::Current&);
            virtual result SetPMTerm2(const std::string& name, double value,
                                     const std::string& movetype,
                                     const std::string& side,
                                     const Ice::Current&);

            virtual result MoveXYZ(double xmov, double ymov, double zmov,
                                   const std::string& side,
                                   const Ice::Current&);

            virtual result MoveXY(double xmov, double ymov,
                                  const std::string& ope,
                                  const std::string& side, const Ice::Current&);

            virtual result RotateCommon(double z, double angle,
                                        double direction,
                                        const std::string& side,
                                        const Ice::Current&);

            virtual result RotateZ(double angle, const std::string& movetype,
                                   const std::string& side,
                                   const Ice::Current&);

            virtual result Move(double x, double y, double z, double rx,
                                double ry, double rz, int dflag,
                                const std::string& movetype,
                                const std::string& OPE, int time,
                                const std::string& side, const Ice::Current&);

            virtual result SetHotspot(double coord1, double coord2,
                                      const Ice::Current&);

            virtual result ClearHotspot(const Ice::Current&);

            virtual result SetOffset(double coord1, double coord2,
                                     const std::string& coordsys,
                                     const Ice::Current&);
            virtual result SetOffsetNew(double coord1, double coord2,
                                     const std::string& coordsys,
                                     const std::string& movetype,
                                     const Ice::Current&);
            virtual result SetOffset2(double coord1, double coord2,
                                     const std::string& system,
                                     const std::string& movetype,
                                     const Ice::Current&);

            virtual result ClearOffset(const Ice::Current&);

            virtual result SetNonSidereal(const nonsidereal & target,
                                          bool override, const Ice::Current&);

            virtual result ClearNonSidereal(bool override, const Ice::Current&);

            virtual result UpdateNonSiderealTarget(double RArate,
                                                   double DECrate,
                                                   const Ice::Current&);

            virtual result SetNonSiderealObject(const std::string & objtype,
				          const nonsidereal & target,
                                          bool override, const Ice::Current&);

            virtual result ClearNonSiderealObject(const std::string & objtype,
                                          bool override, const Ice::Current&);

            virtual result UpdateNonSiderealObject(const std::string & objtype,
                                        double RArate,
                                          double DECrate, const Ice::Current&);

            virtual result SetTarget(double coord1, double coord2,
                                     const std::string& coordsys, double epoch,
                                     float wl, const Ice::Current&);

            virtual result SetStars(const SeqPos & list, const Ice::Current&);
            virtual result SetStarsNew(const SeqNewPos & list, const Ice::Current&);
            virtual result SetStars2(const SeqPos2 & list, const Ice::Current&);

            virtual result ClearStars(const Ice::Current&);

            virtual result PresetTelescope(double rotangle,
                                           const std::string& rotmode,
                                           const std::string& mode,
                                           const std::string& side,
                                           const Ice::Current&);

            virtual result BinocularControl(const std::string& flag,
                                            const Ice::Current&);

            virtual result GetParameter(const SeqDD & list,
                                        const Ice::Current&);

            virtual result SetParameter(const SeqDD & list,
                                        const Ice::Current&);

            virtual result SendWavefront(const SeqWF & wfs,
                                         const std::string& OPE,
                                         const std::string& side,
                                         const Ice::Current&);

            virtual result UpdateTargetWavelength(float wavelength,
                                                  const std::string& side,
                                                  const Ice::Current&);

            virtual result RotReady(bool enable, const std::string& side,
                                    const Ice::Current&);

            virtual result RotHold(const std::string& side,
                                   const Ice::Current&);

            virtual result RotTrack(const std::string& side,
                                    const Ice::Current&);

            virtual result MaximizeWrapTime(bool azflag, bool rotflag,
                                            const std::string& side,
                                            const Ice::Current&);

            virtual result RotServicePosition(double angle,
                                              const std::string& side,
                                              const Ice::Current&);

            virtual result PauseGuiding(const std::string& side,
                                        const Ice::Current&);

            virtual result ResumeGuiding(const std::string& side,
                                         const Ice::Current&);

            virtual result SetGuidingHotspot(double coord1, double coord2,
                                             const std::string& movetype,
                                             const std::string& side,
                                             const Ice::Current&);

            virtual result PauseAO(const std::string& side,
                                   const Ice::Current&);

            virtual result ResumeAO(const std::string& side,
                                    const Ice::Current&);

            virtual result StartAO(const std::string& side,
                                   const Ice::Current&);

            virtual result StopAO(const std::string& reason,
                                  const std::string& side, const Ice::Current&);

            virtual result AcquireRefAO(bool repointFlag,
                                        const std::string& side,
                                        const Ice::Current&);

            virtual result CheckRefAO(const std::string& side,
                                      const Ice::Current&);

            virtual result RefineAO(const std::string& method,
                                    const std::string& side,
                                    const Ice::Current&);

            virtual result RunAO(const std::string& type,
                                 const std::string& side, const Ice::Current&);

            virtual result PresetFlatAO(const std::string& flat,
                                        const std::string& side,
                                        const Ice::Current&);

            virtual result CorrectModesAO(const SeqModes & modes,
                                          const std::string& side,
                                          const Ice::Current&);

            virtual result ModifyAO(int NModes, double Freq, int Nbins,
                                    double TTMod, const std::string& F1spec,
                                    const std::string& F2spec,
                                    const std::string& side,
                                    const Ice::Current&);

            virtual result PresetAO(const std::string& AOMode,
                                    const std::string& side,
                                    const Ice::Current&);

            virtual result OffsetXYAO(double DeltaX, double DeltaY,
                                      const std::string& side,
                                      const Ice::Current&);

            virtual result OffsetZAO(double DeltaZ, const std::string& side,
                                     const Ice::Current&);

            virtual result SetReference(const SeqPos & list,
                                        const Ice::Current&);
            virtual result SetReferenceNew(const SeqNewPos & list,
                                           const Ice::Current&);
            virtual result SetReference2(const SeqPos2 & list,
                                           const Ice::Current&);

            virtual result ClearReference(const Ice::Current&);

            virtual result LogEvent(const std::string& event,
                                    const std::string& description,
                                    const Ice::Current&);

            virtual result GetRotatorTrajectory(double seconds, double interval,
                                                double start,
                                                const std::string& side,
                                                const Ice::Current&);

            virtual result GetRotatorPolynomials(double start, int count,
                                                 double interval, int order,
                                                 const std::string& side,
                                                 const Ice::Current&);

            virtual result Standby(int level, const std::string& side,
                                   const Ice::Current&);

            virtual result RotatePrimary(double distance, double angle,
                                         double direction,
                                         const std::string& side,
                                         const Ice::Current&);

            virtual result GetKFPCoordinates(const SeqPos & list,
                                             const std::string& side,
                                             const Ice::Current&);
            virtual result GetKFPCoordinatesNew(const SeqNewPos & list,
                                                const std::string& side,
                                                const Ice::Current&);
            virtual result GetKFPCoordinates2(const SeqPos2 & list,
                                                const std::string& side,
                                                const Ice::Current&);

            virtual result SetAGWFilter(int filterNumber,
                                        const std::string& side,
                                        const Ice::Current&);

            virtual result SetGuidingBinning(int factor,
                                        const std::string& side,
                                        const Ice::Current&);

            virtual result NSQuery(const std::string & targenName,
                                        const std::string& startDate,
                                        const std::string& startTime,
                                        const std::string& endDate,
                                        const std::string& endTime,
                                        const std::string& interval,
                                        const std::string& fileName,
                                        const Ice::Current&);

            virtual void remove(const Ice::Current&);

        private:
            int m_ddit;
            int m_PFTit;
            std::map<std::string, std::string> m_dd1;
            std::map<std::string, std::string> m_dd2;
            std::vector<std::string> m_PFT01;
            std::vector<std::string> m_poly;
            std::vector<std::string> m_error;
            result m_errorResult;

            //! IIF Target. See IIF Position object. Must be defined before to call PresetTelescope.
            position2 target;

#if 0
            //!flag to enable/disable the GetParameter logging
            bool dd_log_flag;

            //!flag to enable/disable debug information on the logging system.
            bool debug_flag;

            //!flag to enable/disable logging information on the logging system.
            bool logging_flag;

            //!Command counter, increased with every command in execution
            int __cmds;

            //!Saved order in GetRotatorPolynomials
            int __order;

            //!Recursive Ice mutex to protect the command counter operations.
            IceUtil::RecMutex __cmdsMutex;

            //! flags to setup the target, guide stars, hotspot, offset, nonSidereal, and reference.
            bool btarget;
            bool bguidestars;
            bool bhotspot;
            bool boffset;
            bool bnonSidereal;
            bool breference;

            //! The IIF object.
            IIF *anIIF;

            //! IIF Hotspot. See IIF Hotspot class. It is an optional parameter of the PresetTelescope command
            Hotspot *hotspot;

            //! IIF Offset. See IIF Offset class. It is an optional parameter of the PresetTelescope command
            Offset *offset;

            //! list of guide stars. See the IIF Position class.
            std::vector<Position*> guidestars;

            //! non-sidereal object
            NonSidereal *nonSidereal;

            //! AO reference star(s)
            Position *reference;
            vector<Position> LNReference;

            //! dd list to read each time we take an exposure or we use the GetParameter command.
            SeqDD __ddlist;

            //! The Ice communicator
            Ice::CommunicatorPtr __comm;

            //! The Ice property object. Used to get the properties from the configuration file.
            Ice::PropertiesPtr __props;

#endif /* 0 */
            /** The proxy name
            */
            std::string proxyName;

#if 0
            /** @brief Executes the whole bunch of IIF steps involved during command execution and returns a DirtI Result object
             @param aResult IIF Result object
             @param command String to be used by the logging system
             @param log_flag Activates/Deactivates the server logging system.
             @return Result object */
            result execute(Result *aResult, string cmd);
#endif
    };

    std::ostream & operator<< ( std::ostream & os , const DDstruct & DD) ;
    std::ostream & operator<< ( std::ostream & os , const position & pos) ;
    std::ostream & operator<< ( std::ostream & os , const position2 & pos2) ;
    std::istream & operator>> ( std::istream & is , position2 & pos2) ;
    std::ostream & operator<< ( std::ostream & os , const nonsidereal & target) ;
}

