using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Proxy;
using System.Configuration;
using TFSWrappers;

namespace TFSBuildManager
{
    public partial class OptionsForm : Form
    {
        public OptionsForm(BuildStoreWrapper buildStoreWrapper)
        {
            InitializeComponent();
            txtDefaultServer.Text = ConfigurationManager.AppSettings[Constants.DefaultServerAppSettingsKey];
            txtLogFileViewer.Text = ConfigurationManager.AppSettings[Constants.LogFileViewerAppSettingsKey];
            txtBuildTypeFileViewer.Text = ConfigurationManager.AppSettings[Constants.BuildTypeFileViewerAppSettingsKey]; 
            this.buildStoreWrapper = buildStoreWrapper;
        }

        private BuildStoreWrapper buildStoreWrapper;

        private string logFileViewer;

        public string LogFileViewer
        {
            get { return logFileViewer; }
        }

        private string buildTypeFileViewer;

        public string BuildTypeFileViewer
        {
            get { return buildTypeFileViewer; }
        }

        private void btnSelectServer_Click(object sender, EventArgs e)
        {
                DomainProjectPicker domainProjectPicker = new DomainProjectPicker(DomainProjectPickerMode.None);
                if (domainProjectPicker.ShowDialog() == DialogResult.OK)
                {
                    txtDefaultServer.Text = domainProjectPicker.SelectedServer.Name;
                }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
            configFileMap.ExeConfigFilename = Application.ExecutablePath+ ".config";
            Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            configuration.AppSettings.Settings[Constants.DefaultServerAppSettingsKey].Value = txtDefaultServer.Text;
            configuration.AppSettings.Settings[Constants.LogFileViewerAppSettingsKey].Value = txtLogFileViewer.Text;
            configuration.AppSettings.Settings[Constants.BuildTypeFileViewerAppSettingsKey].Value = txtBuildTypeFileViewer.Text;
            configuration.Save();
            logFileViewer = txtLogFileViewer.Text;
            buildTypeFileViewer = txtBuildTypeFileViewer.Text;
        }

        private void btnBrowseViewer_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtLogFileViewer.Text = openFileDialog.FileName;
            }
        }

        private void btnBrowseBuildTypeViewer_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtBuildTypeFileViewer.Text = openFileDialog.FileName;
            }
        }
    }
}