//
//  ObserverClientCpp.h
//  k-observer-iphone
//
//  Created by vvv ооо on 06.09.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef OBSERVER_CLIENT_CPP_H_INCLUDED
#define OBSERVER_CLIENT_CPP_H_INCLUDED

#include "ClientSocket.h"




void AppDelegateSendOnConnectionClosed();

void AppDelegateSendOnReceiveQuit();

void AppDelegateAddNewMessage(TUserInfo userInfo, std::string message);

void AppDelegateOnAutorizationSend();




class TObserverClient
{
public:
    boost::asio::io_service IoService;
    
    boost::thread IoServiceThread;
    
    boost::mutex UpdatingMapMutex;
    
    boost::shared_ptr<TClientSocket> ClientSocket;
    
    boost::shared_ptr<boost::asio::deadline_timer> HeartbeatTimer;
    boost::shared_ptr<boost::asio::deadline_timer> MapHeartbeatTimer;
    boost::signal<void ()> ReceiveCloseSignal;
    
    std::vector<TUserInfo> UserInfoArr;
    std::vector<TUserInfo> AlarmUserInfoArr;
    
    int MapType;
    
    TObserverClient();
    
    void DestroyClientSocket();
    void IoServiceRun();
    void CreateClientSocket(const char* address, const char* port);
    void OnAutorizationSend();
    void UpdateUserInfo(const char* lastName,
                        const char* firstName,
                        const char* middleName,
                        const char* phoneNumber,
                        int uik,
                        int status);
    
    
    void RestartHeartbeatTimer();
    
    void RestartMapHeartbeatTimer();
    
    void OnCloseSocket();
    
    void OnReceiveQuit();
    
    void OnHeartbeat(const boost::system::error_code& e);
    
    void OnMapHeartbeat(const boost::system::error_code& e);
    
    
    void OnReceiveMapUpdate(std::vector<TUserInfo> userInfoArr);
    void OnReceiveAlarmMapUpdate(std::vector<TUserInfo> userInfoArr);
    
    void SwitchMapToAlarm();
    
    void SwitchMapToCommon();
    
    bool IsAlarmMap();
    
    void OnReceiveMessage(TUserInfo userInfo, std::string message);
    
    void SendMessage(const char* msg);
    

    

};


#endif
