using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace BEMSDataGateway
{
    public partial class SettingForm : Form
    {
        public SettingForm()
        {
            InitializeComponent();
        }

        private void SettingFormSave_Click(object sender, EventArgs e)
        {
            Readini Ri = new Readini();
            SaveData();
            if (Form1.Enable_EnglishMode)//영어
            {
                MessageBox.Show("Saved");
            }
            else//한글
            {
                MessageBox.Show("저장되었습니다");
            }
            Close();
        }

        public void SaveData()
        {
            Readini Ri = new Readini();

            Ri.SetIniValue("USER", "UserIP", TbIP.Text);
            Ri.SetIniValue("USER", "UserPort", TbPort.Text);
            Ri.SetIniValue("USER", "UserDB", TbName.Text);
            Ri.SetIniValue("USER", "UserID", TbId.Text);
            Ri.SetIniValue("USER", "UserPW", TbPW.Text);

            Ri.SetIniValue("ICOS", "DGW_IP", DGW_IP.Text);
            Ri.SetIniValue("ICOS", "IP1", IP1.Text);
            Ri.SetIniValue("ICOS", "Port1", PORT1.Text);
            Ri.SetIniValue("ICOS", "IP2", IP2.Text);
            Ri.SetIniValue("ICOS", "Port2", PORT2.Text);

            Ri.SetIniValue("ICOS", "ICOS_ID", ICOS_ID.Text);
            Ri.SetIniValue("ICOS", "ICOS_PASSWORD", ICOS_PASSWORD.Text);
            Ri.SetIniValue("ICOS", "ICOSDB_TYPE", ICOSDB_TYPE.Text);
            Ri.SetIniValue("ICOS", "ICOSDB_NAME", ICOSDB_NAME.Text);
            Ri.SetIniValue("ICOS", "ICOSDB_ID", ICOSDB_ID.Text);
            Ri.SetIniValue("ICOS", "ICOSDB_LOC", ICOSDB_LOC.Text);

            Ri.SetIniValue("ICOS", "ICOSDB_PASSWORD", ICOSDB_PASSWORD.Text);

            Ri.SetIniValue("HeatAndCool", "CoolDayFromgs", CoolDayFrom.Text);
            Ri.SetIniValue("HeatAndCool", "CoolDayTogs", CoolDayTo.Text);
            Ri.SetIniValue("HeatAndCool", "HeatDayFromgs", HeatDayFrom.Text);
            Ri.SetIniValue("HeatAndCool", "HeatDayTogs", HeatDayTo.Text);

            Ri.SetIniValue("PeakInfo", "SiteId", SiteId_txt.Text);
        }

        private void SettingForm_Load(object sender, EventArgs e) //ICOS 정보 추가 해야함
        {
            Readini Ri = new Readini();
            Ri.ReadData();

            TbIP.Text = Ri.UserIP;
            TbPort.Text = Ri.UserPort;
            TbName.Text = Ri.UserDB;
            TbId.Text = Ri.UserID;
            TbPW.Text = Ri.UserPW;

            DGW_IP.Text = Ri.DGW_IP;
            IP1.Text = Ri.IP1;
            IP2.Text = Ri.IP2;
            PORT1.Text = Ri.Port1;
            PORT2.Text = Ri.Port2;

            ICOS_ID.Text = Ri.ICOS_ID;
            ICOS_PASSWORD.Text = Ri.ICOS_PASSWORD;
            ICOSDB_TYPE.Text = Ri.ICOSDB_TYPE;
            ICOSDB_NAME.Text = Ri.ICOSDB_NAME;
            ICOSDB_ID.Text = Ri.ICOSDB_ID;
            ICOSDB_PASSWORD.Text = Ri.ICOSDB_PASSWORD;
            ICOSDB_LOC.Text = Ri.ICOSDB_LOC;
            Englishization.Text = Ri.Enable_EnglishMode.ToString();
            CoolDayFrom.Text = Ri.CoolDayFromgs;
            CoolDayTo.Text = Ri.CoolDayTogs;
            HeatDayFrom.Text = Ri.HeatDayFromgs;
            HeatDayTo.Text = Ri.HeatDayTogs;

            SiteId_txt.Text = Ri.SiteId;
        }

        private void SettingFormExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
    class Readini
    {
        public string UserIP { get; set; }
        public string UserPort { get; set; }
        public string UserDB { get; set; }
        public string UserID { get; set; }
        public string UserPW { get; set; }

        public string DGW_IP { get; set; }
        public string IP1 { get; set; }
        public string Port1 { get; set; }
        public string IP2 { get; set; }
        public string Port2 { get; set; }
        public string ICOS_ID { get; set; }
        public string ICOS_PASSWORD { get; set; }

        public string ICOSDB_TYPE { get; set; }
        public string ICOSDB_NAME { get; set; }
        public string ICOSDB_ID { get; set; }
        public string ICOSDB_PASSWORD { get; set; }
        public string ICOSDB_LOC { get; set; }

        public string MaxPeakValue { get; set; }
        public string MinPeakValue { get; set; }
        public string todayMaxPeakValue { get; set; }
        public string todayMinPeakValue { get; set; }
        public bool Enable_EnglishMode { get; set; }
        public string SiteId { get; set; }

        public string CoolDayFromgs { get; set; }
        public string CoolDayTogs { get; set; }
        public string HeatDayFromgs { get; set; }
        public string HeatDayTogs { get; set; }

        private string filePath;

        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(String section, String key, String val, String filePath);
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(String section, String key, String def, StringBuilder retVal, int size, String filePath);

        public string ReadIniFile(string section, string key, string path)
        {
            StringBuilder sb = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", sb, sb.Capacity, path);
            return sb.ToString();
        }

        public void ReadData()
        {
            FileInfo exefileinfo = new FileInfo(Application.ExecutablePath);
            string fileName = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Login.ini");
            filePath = fileName;

            UserIP = ReadIniFile("USER", "UserIP", filePath);
            UserPort = ReadIniFile("USER", "UserPort", filePath);
            UserDB = ReadIniFile("USER", "UserDB", filePath);
            UserID = ReadIniFile("USER", "UserID", filePath);
            UserPW = ReadIniFile("USER", "UserPW", filePath);
            if (ReadIniFile("USER", "Enable_EnglishMode", filePath) == "true")
            {
                Enable_EnglishMode = true;
            }
            else
            {
                Enable_EnglishMode = false;
            }

            DGW_IP = ReadIniFile("ICOS", "DGW_IP", filePath);
            IP1 = ReadIniFile("ICOS", "IP1", filePath);
            Port1 = ReadIniFile("ICOS", "Port1", filePath);
            IP2 = ReadIniFile("ICOS", "IP2", filePath);
            Port2 = ReadIniFile("ICOS", "Port2", filePath);

            ICOS_ID = ReadIniFile("ICOS", "ICOS_ID", filePath);
            ICOS_PASSWORD = ReadIniFile("ICOS", "ICOS_PASSWORD", filePath);

            ICOSDB_TYPE = ReadIniFile("ICOS", "ICOSDB_TYPE", filePath);
            ICOSDB_LOC = ReadIniFile("ICOS", "ICOSDB_LOC", filePath);
            ICOSDB_NAME = ReadIniFile("ICOS", "ICOSDB_NAME", filePath);
            ICOSDB_ID = ReadIniFile("ICOS", "ICOSDB_ID", filePath);
            ICOSDB_PASSWORD = ReadIniFile("ICOS", "ICOSDB_PASSWORD", filePath);

            MaxPeakValue = ReadIniFile("PeakInfo", "MaxPeakValue", filePath);
            MinPeakValue = ReadIniFile("PeakInfo", "MinPeakValue", filePath);
            todayMaxPeakValue = ReadIniFile("PeakInfo", "todayMaxPeakValue", filePath);
            todayMinPeakValue = ReadIniFile("PeakInfo", "todayMinPeakValue", filePath);

            SiteId = ReadIniFile("PeakInfo", "SiteId", filePath);

            CoolDayFromgs = ReadIniFile("HeatAndCool", "CoolDayFromgs", filePath);
            CoolDayTogs = ReadIniFile("HeatAndCool", "CoolDayTogs", filePath);
            HeatDayFromgs = ReadIniFile("HeatAndCool", "HeatDayFromgs", filePath);
            HeatDayTogs = ReadIniFile("HeatAndCool", "HeatDayTogs", filePath);
        }

        public void SetIniValue(String Section, String Key, String Value)
        {
            FileInfo exefileinfo = new FileInfo(Application.ExecutablePath);
            string fileName = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Login.ini");
            string filePath = fileName;
            WritePrivateProfileString(Section, Key, Value, filePath);
        }
    }
}
