using Markdig;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;


namespace hMarkdown
{
    public partial class Form1 : Form
    {
        public Form1(string fn)
        {
            InitializeComponent();
            this.DragEnter += Form1_DragEnter;
            this.Resize += Form1_Resize;
            
            if (fn.Length>0) {
                tbDosya.Text = fn; 
                DosyaAc(); 
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
             if (this.Width > 200)
                tbDosya.Width = this.Width - 250;
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
                e.Effect = DragDropEffects.Copy;
        }

        private void DosyaAc()
        {
            try {
                if(File.Exists(tbDosya.Text)) {
                     rtbEditor.Text = File.ReadAllText(tbDosya.Text);
                }
            } catch {}            
            RenderHtml();
        }

        private void RenderHtml()
        {
            string contents = rtbEditor.Text;

            var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
            var result = Markdown.ToHtml(contents, pipeline);
            
            var bas = @"<html>
                <head>
                    <meta http-equiv=""X-UA-Compatible"" content=""IE=edge"" />
                    <link rel=""stylesheet"" href=""https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown-light.min.css"">
                    <link rel=""stylesheet"" href=""https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.5/styles/github.min.css"">
                    <script src=""https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.5/highlight.min.js""></script>
                    <script>hljs.initHighlightingOnLoad();</script>
                    <style>
                        .markdown-body {
                            box-sizing: border-box;
                            min-width: 200px;
                            max-width: 980px;
                            margin: 0 auto;
                            padding: 45px;
                        }
                        @media (max-width: 767px) {
                            .markdown-body {
                                padding: 15px;
                            }
                        }
                        table { width: 100%; }
                        img { max-width: 100%; }
                    </style>
                </head>
                <body class=""markdown-body"">";

            result = bas + result + @"
                </body>
                </html>";
            webBrowser.DocumentText = result;

            //webBrowser.Document.Body.Style.font = "font-family: Tahoma, Threbucet, Verdana; font-size: large;";
            //webBrowser.Document.ExecCommand("SelectAll", false, "null");
            //webBrowser.Document.ExecCommand("font-family", false, "Tahoma");
            //webBrowser.Document.ExecCommand("Unselect", false, "null");
        }

        private void tbGozat_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "(*.md)|*.md|Tüm Dosyalar (*.*)|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                tbDosya.Text = openFileDialog.FileName;
                DosyaAc();
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (files.Length > 0)
            {
                tbDosya.Text = files[0];
                DosyaAc();
            }
        }

        private void tbEdit_Click(object sender, EventArgs e)
        {
            if (rtbEditor.Visible)
            {
                rtbEditor.Visible = false;
                webBrowser.Visible = true;
                tbEdit.Text = "Düzenle";
                RenderHtml();
            }
            else
            {
                rtbEditor.Visible = true;
                webBrowser.Visible = false;
                tbEdit.Text = "Görüntüle";
            }
        }

        private void tbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(tbDosya.Text))
                {
                    File.WriteAllText(tbDosya.Text, rtbEditor.Text);
                    MessageBox.Show("Dosya kaydedildi.");
                }
                else
                {
                     // Save as... logic could go here, but for now just warn
                     MessageBox.Show("Lütfen önce bir dosya açın veya oluşturun.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hata: " + ex.Message);
            }
        }

        private void tbHelp_Click(object sender, EventArgs e)
        {
            ShowHelp();
        }

        private void rtbEditor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                ShowHelp();
            }
        }

        private void ShowHelp()
        {
            var helpForm = new HelpForm();
            helpForm.Show();
        }

        private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            string file = e.Url.LocalPath.ToString();

            if (file!="blank")
            {
                
                tbDosya.Text = file.Replace("file:///", ""); ;
                DosyaAc();
            }
        }
    }
}
