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.AllowDrop = true;
            if (fn.Length>0) {
                tbDosya.Text = fn; 
                DosyaAc(); }
        }

        private void DosyaAc()
        {
            string contents = File.ReadAllText(@tbDosya.Text);

            var result = Markdown.ToHtml(contents);
            var bas = @"<html><style type = ""text/css"">
                .markdown-body {
                    color-scheme: light;
                    -ms-text-size-adjust: 100%;
                    -webkit-text-size-adjust: 100%;
                    margin: 0;
                    color: #1f2328;
                    background-color: #fff;
                    font-family: -apple-system, Threbucet, BlinkMacSystemFont, Segoe UI, Noto Sans, Helvetica, Tahoma, Verdana, Arial, sans-serif, ""Apple Color Emoji"", ""Segoe UI Emoji"";
                    font-size: 16px;
                    line-height: 1.5;
                    word-wrap: break-word;}
                </style><body> <div id = ""output"" class=""markdown-body"">";

            result = bas + result + "</div></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);
            foreach (string file in files) Console.WriteLine(file);
        }

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

            if (file!="blank")
            {
                
                tbDosya.Text = file.Replace("file:///", ""); ;
                string contents = File.ReadAllText(@tbDosya.Text);

                var result = Markdown.ToHtml(contents);

                webBrowser.DocumentText = result;
                webBrowser.Document.ExecCommand("SelectAll", false, "null");
                webBrowser.Document.ExecCommand("FontName", false, "Tahoma");
                webBrowser.Document.ExecCommand("Unselect", false, "null");
            }
        }
    }
}
