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;

namespace ISIM
{
    public partial class BugReport : Form
    {
        bool bug; // jedná se o bug nebo nápad na vylepšení
        int currentQuestion = 0;
        List<string> Questions = new List<string>();
        List<string> Answers = new List<string>();

        List<string> SuggestImproveQuestions = new List<string>();
        List<string> ReportButQuestions = new List<string>();
        public BugReport()
        {
            InitializeComponent();
            InitializeQuestions();

            SuggestImprove.Text = Global.LangManager.Translate("suggestImprove");
            ReportBug.Text = Global.LangManager.Translate("reportBUG");
            BugReportBotQuestion.Text = Global.LangManager.Translate("bugReportQuestion1");
            BugReportContinue.Text = Global.LangManager.Translate("next");
            BugReportBack.Text = Global.LangManager.Translate("back");
        }

        private void InitializeQuestions()
        {
            ReportButQuestions.Add(Global.LangManager.Translate("bugQuestion1"));
            ReportButQuestions.Add(Global.LangManager.Translate("bugQuestion2"));
            ReportButQuestions.Add(Global.LangManager.Translate("bugQuestion3"));

            SuggestImproveQuestions.Add(Global.LangManager.Translate("suggestionQuestion1"));
            SuggestImproveQuestions.Add(Global.LangManager.Translate("suggestionQuestion2"));
        }

        private void ReportBug_Click(object sender, EventArgs e)
        {
            bug = true;
            Questions = ReportButQuestions;
            StartReporting();
        }

        private void SuggestImprove_Click(object sender, EventArgs e)
        {
            bug = false;
            Questions = SuggestImproveQuestions;
            StartReporting();
        }

        private void StartReporting()
        {
            ReportBug.Visible = false;
            SuggestImprove.Visible = false;

            BugReportMessageBox.Visible = true;
            BugReportContinue.Visible = true;
            BugReportBack.Visible = true;

            BugReportBotQuestion.Text = Questions[currentQuestion];

            BugReportMessageBox.Focus();
        }

        private void BugReportMessageBox_TextChanged(object sender, EventArgs e)
        {
            if (BugReportMessageBox.TextLength > 5)
                BugReportContinue.Enabled = true;
            else
                BugReportContinue.Enabled = false;
        }

        private void FinishReport()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(Global.myNick + " (" + Environment.UserName + ")" + " is reporting a ");
            sb.AppendLine(bug ? "BUG" : "IMPROVE suggest");
            sb.Append(Login.GoogleTalk.userJid != "" ? "Email: " + Login.GoogleTalk.userJid + Environment.NewLine : "");
            sb.AppendLine("DateTime: " + DateTime.Now.ToString("dd.MM.yyyy   H:mm:ss"));
            sb.AppendLine("Running: " + Environment.OSVersion.VersionString);
            sb.AppendLine("Program version: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
            sb.AppendLine("------------------------------------" + Environment.NewLine);

            for (int i = 0; i < Questions.Count; i++)
            {
                sb.AppendLine((i + 1) + ") " + Questions[i] + " :");
                sb.AppendLine(" >> \"" + Answers[i] + "\"" + Environment.NewLine + Environment.NewLine);
            }

            if (Global.SmtpEmail.SendEmail(Global.developerEmail, sb.ToString()))
            {
                MessageBox.Show(Global.LangManager.Translate("reportForwarded"));
                Close();
            }
            else
            {
                MessageBox.Show(Global.LangManager.Translate("reportForwardError") + "\n" + Global.developerEmail);
                BugReportMessageBox.Text = "Mail to: \"" + Global.developerEmail + "\"" + Environment.NewLine + Environment.NewLine + sb.ToString();
                BugReportMessageBox.Focus();
                BugReportMessageBox.SelectAll();
                BugReportBack.Enabled = false;
                BugReportContinue.Enabled = false;
            }
        }

        private void ChangeAnswer(string answer)
        {
            if (Answers.Count >= currentQuestion + 1)
                Answers[currentQuestion] = answer;
            else
                Answers.Add(answer);
        }

        private void BugReportContinue_Click(object sender, EventArgs e)
        {
            if (currentQuestion + 1 >= Questions.Count)
            {
                ChangeAnswer(BugReportMessageBox.Text);
                FinishReport();
            }
            else
            {
                BugReportBack.Enabled = true;
                BugReportContinue.Enabled = false;

                ChangeAnswer(BugReportMessageBox.Text);

                BugReportMessageBox.Text = "";

                currentQuestion++;
                BugReportBotQuestion.Text = Questions[currentQuestion];
                if (Answers.Count >= currentQuestion + 1)
                    BugReportMessageBox.Text = Answers[currentQuestion];

                if (currentQuestion + 1 >= Questions.Count)
                    BugReportContinue.Text = Global.LangManager.Translate("finish");
                else
                    BugReportContinue.Text = Global.LangManager.Translate("next");
            }
        }

        private void BugReportBack_Click(object sender, EventArgs e)
        {
            if (currentQuestion > 0)
            {
                ChangeAnswer(BugReportMessageBox.Text);

                currentQuestion--;
                BugReportBotQuestion.Text = Questions[currentQuestion];
                BugReportMessageBox.Text = Answers[currentQuestion];

                BugReportContinue.Text = Global.LangManager.Translate("next");

                if (currentQuestion == 0) BugReportBack.Enabled = false;
            }
        }
    }
}
