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;

namespace CobXmlSupport
{
    public partial class frmSettings : Form
    {
        private string UserTmp, CompanyTmp;
        private bool tmpChkNoNsRef;
        private bool tmpChkUniqueVars;
        private bool tmpChkUniqueAttrVars;
        private bool tmpChkUnique;
        private bool tmpChkWrap;
        private bool tmpChkValues;
        private bool tmpChkAnaTag;
        private bool tmpChkCountVars;
        private bool tmpShowTestBtns;
        private bool tmpBackupOwnCode;
        private bool tmpPerforms;
        private bool tmpMvToDisp;
        private bool tmpDisplays;
        private string tmpTxtPrefix;
        private string tmpTxtExpPrefix;
        private string tmpTxtMaxOcc;
        private string txtLogVarNameTmp;
        private string txtLogVarSectTmp;

        //------------
        private GenSetting genSet;
        //    private string aktualFile;
        //    private string filePath;
        private SaverListener sl;

        public string AktualFile
        {
            get
            {
                return txtActualFile.Text;
            }
            set
            {
                txtActualFile.Text = value;
            }
        }

        public string FilePath
        {
            get
            {
                return txtFilePath.Text;
            }
            set
            {
                txtFilePath.Text = value;
            }
        }

        public GenSetting SettingObj
        {
            get
            {
                return genSet;
            }
            set
            {
                genSet = value;
            }
        }

        public SaverListener SL
        {
            get
            {
                return sl;
            }
            set
            {
                sl = value;
            }
        }

        //------------
        public frmSettings()
        {
            InitializeComponent();
        }

        public string LogPost
        {
            get
            {
                return txtLogPost.Text;
            }
            set
            {
                txtLogPost.Text = value;
            }
        }

        public string LogSection
        {
            get
            {
                return txtLogSection.Text;
            }
            set
            {
                txtLogSection.Text = value;
            }
        }


        public string UserName
        {
            get
            {
                return txtUserName.Text;
            }
            set
            {
                txtUserName.Text = value;
            }
        }
 
        public string SettingsCompanyName
        {
            get
            {
                return txtCompanyName.Text;
            }
            set
            {
                txtCompanyName.Text = value;
            }
        }

        public bool ChkCountVars
        {
            get
            {
                return chkCountVars.Checked;
            }
            set
            {
                chkCountVars.Checked = value;
            }
        }

        public bool ChkNoNsRef
        {
            get
            {
                return chkNoNsRef.Checked;
            }
            set
            {
                chkNoNsRef.Checked = value;
            }
        }
        public bool ChkUniqueVars
        {
            get
            {
                return chkUniqueVars.Checked;
            }
            set
            {
                chkUniqueVars.Checked = value;
            }
        }
        public bool ChkUniqueAttrVars
        {
            get
            {
                return chkUniqueAttrVars.Checked;
            }
            set
            {
                chkUniqueAttrVars.Checked = value;
            }
        }
        public bool ChkUnique
        {
            get
            {
                return chkUnique.Checked;
            }
            set
            {
                chkUnique.Checked = value;
            }
        }
        public bool ChkWrap
        {
            get
            {
                return chkWrap.Checked;
            }
            set
            {
                chkWrap.Checked = value;
            }
        }
        public bool ChkValues
        {
            get
            {
                return chkValues.Checked;
            }
            set
            {
                chkValues.Checked = value;
            }
        }
        public bool ChkAnaTag
        {
            get
            {
                return chkAnaTag.Checked;
            }
            set
            {
                chkAnaTag.Checked = value;
            }
        }

        public bool ChkShowTestbtns
        {
            get
            {
                return chkShowTestbtns.Checked;
            }
            set
            {
                chkShowTestbtns.Checked = value;
            }
        }

        public bool ChkBackupOwnCode
        {
            get
            {
                return chkBackupOwnCode.Checked;
            }
            set
            {
                chkBackupOwnCode.Checked = value;
            }
        }

        public bool Performs
        {
            get
            {
                return chkPerforms.Checked;
            }
            set
            {
                chkPerforms.Checked = value;
            }
        }

        public bool MvToDisp
        {
            get
            {
                return chkMvToDisp.Checked;
            }
            set
            {
                chkMvToDisp.Checked = value;
            }
        }

        public bool Displays
        {
            get
            {
                return chkDisplays.Checked;
            }
            set
            {
                chkDisplays.Checked = value;
            }
        }
        
        
        public string TxtMaxOcc
        {
            get
            {
                return txtMaxOcc.Text;
            }
            set
            {
                txtMaxOcc.Text = value;
            }
        }
        public string TxtPrefix
        {
            get
            {
                return txtPrefix.Text;
            }
            set
            {
                txtPrefix.Text = value;
            }
        }

        public string TxtExpPrefix
        {
            get
            {
                return txtExpPrefix.Text;
            }
            set
            {
                txtExpPrefix.Text = value;
            }
        }
        public bool VisaFilVal { set {
                btnChooseFile.Visible = value;
                btnChooseFile.Refresh();
            } }

        private void btnSave_Click(object sender, EventArgs e)
        {
            sl.Subscribe(this.genSet);

            S.ettingActualFile = this.AktualFile;
            S.ettingAnaTag = Properties.Settings.Default.AnaTag = chkAnaTag.Checked;
            S.ettingCompany = Properties.Settings.Default.CompanyName = txtCompanyName.Text;
            S.ettingCountVars = Properties.Settings.Default.CountVars = chkCountVars.Checked;
            S.ettingDisplays = chkDisplays.Checked;
            S.ettingExpPrefix = Properties.Settings.Default.ExpPrefix = txtExpPrefix.Text;
            S.ettingFilePath = this.FilePath;
            S.ettingLogSectName = Properties.Settings.Default.LogSectName = txtLogSection.Text;
            S.ettingLogVarName = Properties.Settings.Default.LogVarName = txtLogPost.Text;
            S.ettingMaxOcc = Properties.Settings.Default.MaxOcc = txtMaxOcc.Text;
            S.ettingMvToDisp = this.chkMvToDisp.Checked;
            S.ettingNoNsRef = Properties.Settings.Default.NoNsRef = chkNoNsRef.Checked;
            S.ettingPerform = this.chkPerforms.Checked;
            S.ettingPrefix = Properties.Settings.Default.Prefix = txtPrefix.Text;
            S.ettingShowTestbtns = Properties.Settings.Default.ShowTestbtns = chkShowTestbtns.Checked;
            S.ettingUnique = Properties.Settings.Default.Unique = chkUnique.Checked;
            S.ettingUniqueAttrVars = Properties.Settings.Default.UniqueAttrVars = chkUniqueAttrVars.Checked;
            S.ettingUniqueVars = Properties.Settings.Default.UniqueVars = chkUniqueVars.Checked;
            S.ettingUserCodeBcup = Properties.Settings.Default.BackupOwnCode = chkBackupOwnCode.Checked;
            S.ettingUserName = Properties.Settings.Default.UserName = txtUserName.Text;
            S.ettingValues = Properties.Settings.Default.Values = chkValues.Checked;
            S.ettingWrap = Properties.Settings.Default.Wrap = chkWrap.Checked;

            Properties.Settings.Default.Save();

            this.genSet.GetSettings();

            this.sl.UnSubscribe(this.genSet);
            this.Close();
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            this.AktualFile = S.ettingActualFile;
            this.FilePath = S.ettingFilePath;
            tmpChkAnaTag = chkAnaTag.Checked = S.ettingAnaTag;
            tmpChkCountVars = chkCountVars.Checked = S.ettingCountVars;
            tmpBackupOwnCode = chkBackupOwnCode.Checked = S.ettingUserCodeBcup;
            tmpDisplays = chkDisplays.Checked = S.ettingDisplays;
            tmpTxtMaxOcc = txtMaxOcc.Text = S.ettingMaxOcc;
            tmpMvToDisp = chkMvToDisp.Checked = S.ettingMvToDisp;
            tmpChkNoNsRef = chkNoNsRef.Checked = S.ettingNoNsRef;
            tmpPerforms = chkPerforms.Checked = S.ettingPerform;
            tmpTxtPrefix = txtPrefix.Text = S.ettingPrefix;
            tmpTxtExpPrefix = txtExpPrefix.Text = S.ettingExpPrefix;
            tmpChkUnique = chkUnique.Checked = S.ettingUnique;
            tmpChkUniqueAttrVars = chkUniqueAttrVars.Checked = S.ettingUniqueAttrVars;
            tmpChkUniqueVars = chkUniqueVars.Checked = S.ettingUniqueVars;
            tmpChkValues = chkValues.Checked = S.ettingValues;
            tmpChkWrap = chkWrap.Checked = S.ettingWrap;
            tmpShowTestBtns = chkShowTestbtns.Checked = S.ettingShowTestbtns;

            txtLogVarNameTmp = txtLogPost.Text = S.ettingLogVarName;
            txtLogVarSectTmp = txtLogSection.Text = S.ettingLogSectName;

            UserTmp = txtUserName.Text = S.ettingUserName;
            CompanyTmp = txtCompanyName.Text = S.ettingCompany;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ofdChooseFild.Title = "Välj en befintlig xml-fil";
            ofdChooseFild.FileName = this.txtActualFile.Text;
            ofdChooseFild.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofdChooseFild.Filter= "xml files (*.xml)|*.xml|All files (*.*)|*.*";
            ofdChooseFild.Multiselect = false;
            if (ofdChooseFild.ShowDialog() == DialogResult.OK)
            {
                txtActualFile.Text = ofdChooseFild.SafeFileName;
                txtFilePath.Text = System.IO.Path.GetDirectoryName(ofdChooseFild.FileName);
                txtActualFile.Refresh();
                txtFilePath.Refresh();
            }
            else MessageBox.Show("Programmet kommer att krasha om ingen fil väljs!");

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            chkAnaTag.Checked = tmpChkAnaTag;
            chkCountVars.Checked = tmpChkCountVars;
            chkBackupOwnCode.Checked = tmpBackupOwnCode; 
            chkDisplays.Checked = tmpDisplays;
            txtMaxOcc.Text = tmpTxtMaxOcc;
            chkMvToDisp.Checked = tmpMvToDisp;
            chkNoNsRef.Checked = tmpChkNoNsRef;
            chkPerforms.Checked = tmpPerforms;
            txtPrefix.Text = tmpTxtPrefix;
            txtExpPrefix.Text = tmpTxtExpPrefix;
            chkUnique.Checked = tmpChkUnique;
            chkUniqueAttrVars.Checked = tmpChkUniqueAttrVars;
            chkUniqueVars.Checked = tmpChkUniqueVars;
            chkValues.Checked = tmpChkValues;
            chkWrap.Checked = tmpChkWrap;
            chkShowTestbtns.Checked = tmpShowTestBtns;
            txtLogPost.Text = txtLogVarNameTmp;
            txtLogSection.Text = txtLogVarSectTmp;

            txtUserName.Text = UserTmp;
            txtCompanyName.Text = CompanyTmp;
            this.Close();
        }

    }
}
