// Start =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// General information section.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// $Author:yongkiang$ User who last changed the file
// $Date:1/2/2013 12:16:36 PM$ Date and time of last check in
// $Revision:1.0$ Visual SourceSafe version number
// $Workfile::   MaintenancePage.h                                     $ Filename
// End =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// {{AFX_INCLUDES()
#include "MyTreeCtrl.h"
#include "structure.h"
#include "MyPropertyPage.h"
#include "ShadeButtonST.h"
#include <map>
// }}AFX_INCLUDES
#if !defined(AFX_MAINTENANCEPAGE_H__61C5CDF7_71EB_11D6_A7FC_005004C029CA__INCLUDED_)
#define AFX_MAINTENANCEPAGE_H__61C5CDF7_71EB_11D6_A7FC_005004C029CA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifdef  BUILD_GUIDLL
#define     DLLCLASS    __declspec(dllexport)
#else
#define     DLLCLASS    __declspec(dllimport)
#endif

#include "MainSheet.h"
class CMotionController;
class CInputController;
class COutputController;

// MaintenancePage.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CMaintenancePage dialog

class DLLCLASS CMaintenancePage : public CMyPropertyPage // public CPropertyPage
{
DECLARE_DYNCREATE(CMaintenancePage)

// Construction
public:
    void         ExpandMotorTree(bool bEnable = true);
    virtual void CustomPageSettings();
    CString      GetModuleName();
// void SetModule(CModule *pModule);
//  set the module to display and pass the module number to Maintenance module page
    void SetModule(stModule *pModule, int iModuleNo);
    CMaintenancePage();
    ~CMaintenancePage();

// Dialog Data
// {{AFX_DATA(CMaintenancePage)
    enum {
        IDD = IDD_MAINTENANCE_PAGE
    };
    CShadeButtonST m_nextpreBtn;
    CShadeButtonST m_homeBtn;
    CShadeButtonST m_mtronBtn;
    CShadeButtonST m_mtroffBtn;
    CShadeButtonST m_moveBtn;
    CShadeButtonST m_stopBtn;
    CShadeButtonST m_saveBtn;
    CShadeButtonST m_output9Btn;
    CShadeButtonST m_output8Btn;
    CShadeButtonST m_output7Btn;
    CShadeButtonST m_output6Btn;
    CShadeButtonST m_output5Btn;
    CShadeButtonST m_output4Btn;
    CShadeButtonST m_output3Btn;
    CShadeButtonST m_output2Btn;
    CShadeButtonST m_output16Btn;
    CShadeButtonST m_output15Btn;
    CShadeButtonST m_output14Btn;
    CShadeButtonST m_output12Btn;
    CShadeButtonST m_output11Btn;
    CShadeButtonST m_output10Btn;
    CShadeButtonST m_output1Btn;
    CShadeButtonST m_output13Btn;
    CMyTreeCtrl    m_treeCtrl;
    double         m_currStartSpeed;
    double         m_currMaxSpeed;
    double         m_currAccel;
    double         m_percentage;
    double         m_currDeccel;
// }}AFX_DATA

// Overrides
// ClassWizard generate virtual function overrides
// {{AFX_VIRTUAL(CMaintenancePage)
public:
    virtual BOOL OnSetActive();
    virtual BOOL OnKillActive();
    virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// }}AFX_VIRTUAL

// Implementation
protected:
// Generated message map functions
// {{AFX_MSG(CMaintenancePage)
    virtual BOOL OnInitDialog();
    afx_msg void ShowNotification(LPNMHDR pnmhdr, LRESULT *pLResult);
    afx_msg void OnTimer(UINT nIDEvent);
    afx_msg void OnDestroy();
    afx_msg void OnHomeButton();
    afx_msg void OnMoveButton();
    afx_msg void OnStopButton();
    afx_msg void OnSaveButton();
    afx_msg void OnNextPreButton();
    afx_msg void OnMtrOffButton();
    afx_msg void OnMtrOnButton();
// }}AFX_MSG
    afx_msg void OnOutputButtonClick(UINT nID);
    afx_msg void OnSpeedRadioButtonClick(UINT nID);
    DECLARE_MESSAGE_MAP()

    virtual void DisplayIn2ndLang();

private:
//  bool IsCurrentFieldValid(CString csPath, CString csTableName, CString csFieldName);
    bool IsOutputDisplayEnable(CString csOutputName);
    bool IsInputDisplayEnable(CString csInputName);
    void GetIODisplayData();
    bool GetMotorPosSaveTolerence(CString csCurrPosName, double &dMaxOffset, double &dMinOffset, CString csCurrMotorName);
    bool IsCheckTolerenceEnable(CString csCurrPosName, CString csCurrMotorName);
    bool CheckIfDisplayEnable(CString csCurrPosName);
    bool CheckIfTeachEnable(CString csCurrPosName, CString csCurrMotorName);
    void LockButtonsDuringMachineIsRunning();
    bool UpdateCurrentModule(bool SavingSetting = true);
//  void CheckSecurityLevel();
    void DisplaySwitchToCurrentMotorItems();
    void RefreshCurrentModuleMotorInputSignals();
    void RefreshCurrentModuleDigitalOutputSignals();
    void RefreshCurrentModuleDigitalInputSignals();
    void GetCurrentMotorIndexId();
    void GetCurrentMotorPosId();
    void GetCurrentMotorId();
    void SetCurrentMotorSpeedParameter();
    void SetCurrentModuleMotorItems();
    void SetCurrentModuleDigitalOutputTexts();
    void SetCurrentModuleDigitalInputTexts();
    void InitMotorTree();
    void InitIconButtons();
    void LoadLedBitmap();
    void CreateTemporaryMotorBuffer();
    void CreateMotorTreeImageList();
    void CheckRecipeSelected();                                                    // Enable or disable controls base on Recipe Selected
    void EnableDisableSaveButton();                                                // enable save button if recipe is selected and teach current motor is enable
    void CheckUserLevel();                                                         // Enable or disable controls base on User level

    void initMaintenanceIOModules();                                               // Init Maintenance Input/Output Modules attribtes
    void HideExtraInputItems();                                                    // Hide extra Input Items
    void HideExtraOutputItems();                                                   // Hide extra Output Items
    void InitPage1MaintIpAttributes();                                             // init page 1 maintenance input attributes
    void CalNoOfInputModule();                                                     // calculate number of input modules
    void InitPage1MaintOpAttributes();                                             // init page 1 maintenance output attributes
    void CalNoOfOutputModule();                                                    // calculate number of output modules
    void SaveMotorPos(CString csPackageName, bool bGoldenData = false);            // save motor pos
    void SaveMotorSpeedParameter(CString csPackageName, bool bGoldenData = false); // save motor speed setting
    void GetMotorPos(CString csPackageName);                                       // get motor pos and update to text box
    void GetMotorSpeedParameter(CString csPackageName);                            // get motor speed setting and update to text box
    bool IsMachineRunning();                                                       // check if machine is running
    void ProcessMaintMsg();                                                        // process Maintenance Message and print to text box

//  Indicate that output has been moved before
    void SetMvOpFlag();

//  Indicate that motor has been moved before
    void SetMvMtrFlag();

//  Set all motor input signal to off when there is no motor in current module
    void OffAllMtrInputSignals();

//  Display motor final position and return final motor position
    double DisplayFinalMtrPos();

//  clear all motor text when module has no motor
    void ClearAllMtrText();

    int GetSubField(CString csPosName, std::map<int, CString>  &strItem);
//  Get sub Motor Position and update to control
    void GetSubFieldPosValue();

//  enable motor button
    void EnableMotorButton();

//  Disable motor button
    void DisableMotorButton();

//  initialised speed radio button
    void InitSpdRadioBtn();

    CImageList* m_pImageList;
    CString     m_csCurrMotorName;
    CString     m_csCurrPosName;
    CString     m_csCurrIndexName;
    CString     m_csSubPosName;

// Gary 22Dec10, V1.23.02, support multilanguage display
    CString m_csDisplayCurrMotorName;
    CString m_csDisplayCurrPosName;
    CString m_csDisplaySubPosName;

    int     m_currMotor, m_currPos, m_currIndex, m_currSpeedMode;
    int     m_iPrevSelectedPos;
    int     m_iPrevSelectedMotor;
    int   * m_pCurrUserLevel;
    int     m_mcState;
    CBitmap m_ledOnBtm;
    CBitmap m_ledOffBtm;

    typedef struct {
        double m_startSpeed;
        double m_maxSpeed;
        double m_accel;
        double m_percentage;
    }MOTOR_SPEED_ELEMENT;

    typedef struct {
        double m_calculated;
        double m_offset;
        double m_total;
    }MOTOR_POS_ELEMENT;

#define MAX_POS     8;
#define NUM_SPEED_MODE 8;

    typedef struct {
        MOTOR_POS_ELEMENT m_pos[8];
        MOTOR_SPEED_ELEMENT m_speed[8];
    }MOTOR_PARAMETER_SETTING;

    stModule          *m_stCurrentModule;
    CInputController  *m_inputController;
    COutputController *m_outputController;
    CMotionController *m_motionController;

    int m_iModuleNo;           // to store whiich module

//  Maintenance Input/Output Modules attribtes
    int m_iInputStartNo;       // input module start number for UI
    int m_iInputStopNo;        // input module stop number for UI
    int m_iLastInputModuleNo;  // last input module number for UI

    int m_iOutputStartNo;      // output module start number for UI
    int m_iOutputStopNo;       // output module stop number for UI
    int m_iLastOutputModuleNo; // last output module number for UI

/// 0 = Use MotorPos Table else 1 = Use MotorPosition Table
    bool m_bUseMotorPosTable;

    bool m_bReceiveMaintMes; // flag to indicate received message

    enum MTR_POS_CAL_VALUES
    {
        CALCULATED_VALUE,
        OFFSET_VALUE,
        SPEED_CUTOFF_DISTANCE,
        MAX_MTR_POS_CAL_VALUES,
    };

    enum MTR_SPEED_VALUES
    {
        START_SPEED,
        MAX_SPEED,
        ACC_RATE,
        DEC_RATE,
        PERCENTAGE,
        MAX_SPEED_VALUES,
    };

    enum SPEED_SETTING
    {
        SLOW_SPEED,
        NORMAL_SPEED,
        MAX_SPEED_SETTING,
    };

    enum
    {
        MAX_MTR_STATUS_LED =  6,
    };

    bool m_bBlink; // blink maintenance message flag
    bool m_bMvMtr; // flag to remember home or move motor

//  true = save normal or slow speed profile in mitpkg.mdb in MotorSpeed table
    bool m_bSaveSpdProfile;

    CString m_csMaintMes; // maintenance message string

    CString m_csMotorPos[MAX_MTR_POS_CAL_VALUES];
    CString m_csMotorSpeed[MAX_SPEED_VALUES];
    CString m_csSpeedSetting[MAX_SPEED_SETTING];
    CString m_csMotionStatus[MAX_MOTION_STATUS];

    CImageList m_imageListSmall; // image list for list box

    const static char* MANUAL_POS;

    bool *m_bInputDisplayStates;
    int  *m_nEnumIndexForDisplayedInput;

    bool *m_bOutputDisplayStates;
    int  *m_nEnumIndexForDisplayedOutput;

    int m_nNoOfInputDisplay;
    int m_nNoOfOutputDisplay;

    double m_dSelectedMtrCalPosn;
    double m_dSelectedMtrOffsetPosn;

    int    m_nSelectedMtrSpeedMode;
    double m_dSelectedMtrSpeedPercentage;

// Gary 7Sep09, added option to hide motor positions.
    bool m_bHideMotorPosition;

// Gary 12May10, cater customize page settings
    bool m_bExpandMotorTree;

//  true = Off Motor Disable Home and Move Button
    bool m_bOffMtrDisableHmMvBtn;
};

// {{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MAINTENANCEPAGE_H__61C5CDF7_71EB_11D6_A7FC_005004C029CA__INCLUDED_)
