﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CLoggerCs;

namespace BasicLogger
{
    public partial class BasicLogger : Form
    {
        //----------------------------------------
        // Create a CLogger class instance
        //----------------------------------------
        CLogger logger = new CLogger();

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public short g_appid = 0;                             // ID for application access

        public BasicLogger()
        {
            InitializeComponent();
        }

        //================================================================================
        // Initial Settings
        //================================================================================
        private void Form1_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Update the initial display of device name
            //----------------------------------------
            textBox_devicename.Text = "AIO000";

            //----------------------------------------
            // Create the combobox for screen size
            //----------------------------------------
            comboBox_openmode.Items.Add("Minimize");
            comboBox_openmode.Items.Add("Normal");
            comboBox_openmode.Items.Add("Maximize");

            //----------------------------------------
            // Change the selection of screen size combobox to Minimize
            //----------------------------------------
            comboBox_openmode.SelectedIndex = 0;
            return;
        }

        //================================================================================
        // It is the process when the LoggerOpenApplication button is clicked
        // Launch the app and enable access
        //================================================================================
        private void button_LoggerOpenApplication_Click(object sender, EventArgs e)
        {
            string device_name;     // Device name
            ushort openmode;        // Screen size
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get device name from textbox
            //----------------------------------------
            device_name = textBox_devicename.Text;
            //----------------------------------------
            // Get screen size from combobox
            //----------------------------------------
            openmode = (ushort)comboBox_openmode.SelectedIndex;
            //----------------------------------------
            // Initialize the application
            //----------------------------------------
            ret = logger.OpenApplication(device_name, out g_appid, openmode);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            logger.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_returnnum.Text = string.Format("LoggerOpenApplication = {0} : {1} ", ret, error_string);
            return;
        }

        private void button_LoggerSetDeviceName_Click(object sender, EventArgs e)
        {
            string device_name;     // Device name
            int    ret;             // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get device name from textbox
            //----------------------------------------
            device_name = textBox_devicename.Text;
            //----------------------------------------
            // Set the device name
            //----------------------------------------
            ret = logger.SetDeviceName(g_appid, device_name);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            logger.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_returnnum.Text = string.Format("LoggerSetDeviceName = {0} : {1} ", ret, error_string);
            return;
        }

        //================================================================================
        // It is the process when the LoggerImportSettingFile button is clicked
        // Import the settings file
        //================================================================================
        private void button_LoggerImportSettingFile_Click(object sender, EventArgs e)
        {
            string filepath;        // File path
            int    ret;             // For return code
            string error_string;    // Error code string
            uint   status;          // For status storage

            //----------------------------------------
            // Get setting file path from the textbox
            //----------------------------------------
            filepath = textBox_importfile.Text;
            //----------------------------------------
            // Import the settings file
            //----------------------------------------
            ret = logger.ImportSettingFile(g_appid, filepath, out status);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            logger.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_returnnum.Text = string.Format("LoggerImportSettingFile = {0} : {1} \r\nstatus=0x{2:X16}", ret, error_string, status);
            return;
        }

        //================================================================================
        // It is the process when the ...button is clicked
        // Displays a dialog for selecting a configuration file
        //================================================================================
        private void button_importfilepath_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Do Not check if the file name exists
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // If the Then file path exists, Set the file path To the initial path In the dialog.
            //----------------------------------------
            if (textBox_importfile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_importfile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // If the file path does not exist, set MyDocuments to the initial path in the dialog
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // Display the selected path
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_importfile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
            return;
        }

        //================================================================================
        // It is the process when the LoggerSaveDataFile button is clicked
        // Saving the data file
        //================================================================================
        private void button_LoggerSaveDataFile_Click(object sender, EventArgs e)
        {
            string filepath;        // File path
            int ret;                // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Get setting file path from the textbox
            //----------------------------------------
            filepath = textBox_datafile.Text;
            //----------------------------------------
            // Data file saving
            //----------------------------------------
            ret = logger.SaveDataFile(g_appid, filepath, 0, 0);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            logger.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_returnnum.Text = string.Format("LoggerSaveDataFile = {0} : {1} ", ret, error_string);
            return;
        }

        //================================================================================
        // It is the process when the ...button is clicked
        // Displays a dialog for selecting a configuration file
        //================================================================================
        private void button_datafilepath_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Do Not check if the file name exists
            //----------------------------------------
            openFileDialog1.CheckFileExists = false;
            //----------------------------------------
            // If the Then file path exists, Set the file path To the initial path In the dialog.
            //----------------------------------------
            if (textBox_datafile.Text.Length > 0)
            {
                openFileDialog1.InitialDirectory = textBox_datafile.Text;
                openFileDialog1.FileName = openFileDialog1.SafeFileName;
            }
            //----------------------------------------
            // If the file path does not exist, set MyDocuments to the initial path in the dialog
            //----------------------------------------
            else
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFileDialog1.FileName = "";
            }
            //----------------------------------------
            // Display the selected path
            //----------------------------------------
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_datafile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName);
            }
            return;
        }

        //================================================================================
        // It is the process when the Logging Start button is clicked
        // Start the application input and output
        //================================================================================
        private void button_LoggerStartOperation_Click(object sender, EventArgs e)
        {
            string error_string;        // Error code string
            int    ret;                 // For return code
            uint   status;              // For status storage

            //----------------------------------------
            // Close file
            //----------------------------------------
            ret = logger.GraphWindowOperation(g_appid, (ushort)CLoggerConst.LOGGER_PARAM_GRAPH_CLOSE, (ushort)CLoggerConst.LOGGER_PARAM_FILE, 0);
            if (ret != 0)
            {
                //----------------------------------------
                // Get error string
                //----------------------------------------
                logger.GetErrorString(ret, out error_string);
                //----------------------------------------
                // Display error code and message
                //----------------------------------------
                textBox_returnnum.Text = string.Format("LoggerGraphWindowOperation = {0} : {1} ", ret, error_string);
                return;
            }
            //----------------------------------------
            // Specify the buffer mode (No mode)
            //----------------------------------------
            ret = logger.SetAcquiredMode(g_appid, (ushort)CLoggerConst.LOGGER_PARAM_MODE_BASIC);
            if (ret != 0)
            {
                //----------------------------------------
                // Get error string
                //----------------------------------------
                logger.GetErrorString(ret, out error_string);
                //----------------------------------------
                // Display error code and message
                //----------------------------------------
                textBox_returnnum.Text = string.Format("LoggerSetAcquiredMode = {0} : {1} ", ret, error_string);
                return;
            }
            //----------------------------------------
            // Start the application input and output
            //----------------------------------------
            ret = logger.StartOperation(g_appid, out status);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            logger.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_returnnum.Text = string.Format("LoggerStartOperation = {0} : {1} \r\nstatus = {2}", ret, error_string, status);
            return;
        }

        //================================================================================
        // It is the process when the LoggerGetApplicationStatus button is clicked
        // Get the device status
        //================================================================================
        private void button_LoggerGetApplicationStatus_Click(object sender, EventArgs e)
        {
            string status_string = "";   // Status string
            string error_string;         // Error code string
            int ret;                     // For return code
            uint status;                 // For status storage

            //----------------------------------------
            // Get the device status
            //----------------------------------------
            ret = logger.GetApplicationStatus(g_appid, out status);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            logger.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_returnnum.Text = string.Format("LoggerGetApplicationStatus = {0} : {1} ", ret, error_string);
            //----------------------------------------
            // If the status acquisition function is completed successfully
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // Get status string
                //--------------------
                switch (status)
                {
                    case (uint)CLoggerConst.LOGGER_STATUS_STOP:
                        status_string = "Stop";
                        break;
                    case (uint)CLoggerConst.LOGGER_STATUS_START_TRG:
                        status_string = "Wait the start trigger";
                        break;
                    case (uint)CLoggerConst.LOGGER_STATUS_BUSY:
                        status_string = "Operating";
                        break;
                    default:
                        status_string = "Unknown status";
                        break;
                }
                //----------------------------------------
                // Display the status
                //----------------------------------------
                textBox_status.Text = string.Format("{0} ({1})", status, status_string);
            }
            return;
        }

        //================================================================================
        // It is the process when the Logging Stop button is clicked
        // Stop the application input and output
        //================================================================================
        private void button_LoggerStopOperation_Click(object sender, EventArgs e)
        {
            int ret;                // For return code
            string error_string;    // Error code string

            //----------------------------------------
            // Stop the application input and output
            //----------------------------------------
            ret = logger.StopOperation(g_appid);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            logger.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_returnnum.Text = string.Format("LoggerStopOperation = {0} : {1} ", ret, error_string);
            return;
        }

        //================================================================================
        // It is the process when the LoggerCloseApplication button is clicked
        // Stop the app and disable access
        //================================================================================
        private void button_LoggerCloseApplication_Click(object sender, EventArgs e)
        {
            uint status = 0;        // For status storage
            int ret;                // For return code
            string error_string;    // Error code string
            string status_string;   // Status string

            //----------------------------------------
            // Stop the application and disable access
            //----------------------------------------
            ret = logger.CloseApplication(g_appid, out status);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            logger.GetErrorString(ret, out error_string);
            if (ret != 0)
            {
                //----------------------------------------
                // Display error code and message
                //----------------------------------------
                textBox_returnnum.Text = string.Format("LoggerCloseApplication = {0} : {1}", ret, error_string);
                return;
            }
            //----------------------------------------
            // Get status string
            //----------------------------------------
            switch (status)
            {
                case (uint)CLoggerConst.LOGGER_STATUS_SUCCESS:
                    status_string = "Successful completion";
                    break;
                case (uint)CLoggerConst.LOGGER_STATUS_FAILURE:
                    status_string = "C-LOGGER exit error";
                    break;
                default:
                    status_string = "Unknown status";
                    break;
            }
            //----------------------------------------
            // Display error code and message
            //----------------------------------------
            textBox_returnnum.Text = string.Format("LoggerCloseApplication = {0} : {1} \r\nstatus = {2} : {3}", ret, error_string, status, status_string);
            return;
        }

        //================================================================================
        // It is the process when the Close button is clicked
        // Exit the application
        //================================================================================
        private void button_close_Click(object sender, EventArgs e)
        {
            uint status = 0;      // For status storage

            //----------------------------------------
            // Close the file(Considering that C-LOGGER may have exited, do not check the result)
            //----------------------------------------
            logger.GraphWindowOperation(g_appid, (ushort)CLoggerConst.LOGGER_PARAM_GRAPH_CLOSE, (ushort)CLoggerConst.LOGGER_PARAM_FILE, 0);
            //----------------------------------------
            // Stop the application and disable access
            //----------------------------------------
            logger.CloseApplication(g_appid, out status);
            //----------------------------------------
            // Exit the dialog
            //----------------------------------------
            this.Close();
            return;
        }
    }
}
