using System;
using System.Globalization;
using FastReport.Dialog;
using System.Windows.Forms;
using static FastReport.Web.Constants;

namespace FastReport.Web
{
    public partial class Dialog
    {

        /// <summary>
        /// Gets or sets date format in html input type="date"
        /// </summary>
        public static string DatePickerFormat {
            get;
            set;
        } = DEFAULT_DATE_PICKER_FORMAT;

        /// <summary>
        /// Gets or sets time format in html input type="time"
        /// </summary>
        public static string TimePickerFormat
        {
            get;
            set;
        } = DEFAULT_TIME_PICKER_FORMAT;


        private void DateTimePickerChangeDate(DateTimePickerControl dp, string value)
        {
            DateTime oldValue = dp.Value;
            DateTime parsedValue = DateTime.Parse(value);
            DateTime newDateTime = new DateTime(
                parsedValue.Year,
                parsedValue.Month,
                parsedValue.Day,
                oldValue.Hour,
                oldValue.Minute,
                oldValue.Second,
                oldValue.Kind);
            dp.Value = newDateTime;
            dp.OnValueChanged(null);
        }

        private void DateTimePickerChangeTime(DateTimePickerControl dp, string value)
        {
            DateTime oldValue = dp.Value;
            DateTime parsedValue = DateTime.Parse(value);
            DateTime newDateTime = new DateTime(
                oldValue.Year,
                oldValue.Month,
                oldValue.Day,
                parsedValue.Hour,
                parsedValue.Minute,
                parsedValue.Second,
                oldValue.Kind);
            dp.Value = newDateTime;
            dp.OnValueChanged(null);
        }

        private string OnChangeDate(DateTimePickerControl control, bool needReload = true)
        {
            return OnChangeDateTimePicker(control, "-date", needReload);
        }

        private string OnChangeTime(DateTimePickerControl control, bool needReload = true)
        {
            return OnChangeDateTimePicker(control, "-time", needReload);
        }

        private string OnChangeDateTimePicker(DateTimePickerControl control, string suffix, bool needReload)
        {
            return $"onchange=\"{GetEvent(ONCHANGE, control.Name + suffix, needReload ? SILENT_RELOAD : DIALOG, $"this.value")}\"";
        }

        private string GetDateTimePickerHtml(DateTimePickerControl control)
        {
            control.FillData();
            ControlFilterRefresh(control);
            string id = GetControlID(control);

            string date = string.Empty;
            string time = string.Empty;
            string disabled = control.Enabled ? "" : "disabled";
            bool needUpdate = NeedRefresh(control);
            if (control.Format == DateTimePickerFormat.Short)
            {
                // we draw only date
                date = $"<input style=\"{GetDateTimePickerStyle(control)}\"" +
                    $" type=\"date\" value=\"{control.Value.ToString(DatePickerFormat)}\"" +
                    $" {OnChangeDate(control, needUpdate)}" +
                    $" id=\"{id}-date\" {disabled} required/>";

            }
            else if (control.Format == DateTimePickerFormat.Time)
            {
                // we draw only time

                var timeValue = control.Value.ToString(TimePickerFormat);
                time = $"<input style=\"{GetDateTimePickerStyle(control)} min-width:5rem;\"" +
                    $" type=\"time\" value=\"{timeValue}\"" +
                    $" {OnChangeTime(control, needUpdate)}" +
                    $" id=\"{id}-time\" {disabled} required/>";
            }
            else
            {
                // we draw date and time in one container

                date = $"<input style=\"{GetControlFont(control.Font)} {GetBackColor(control.BackColor)} border-right-width:0px; padding-right:0px\"" +
                    $" type=\"date\" value=\"{control.Value.ToString(DatePickerFormat)}\"" +
                    $" {OnChangeDate(control, needUpdate)}" +
                    $" id=\"{id}-date\" {disabled} required/>";

                var timeValue = control.Value.ToString(TimePickerFormat);
                time = $"<input style=\"{GetControlFont(control.Font)} {GetBackColor(control.BackColor)} border-left-width:0px; padding-left:0px; min-width:5rem;\"" +
                    $" type=\"time\" value=\"{timeValue}\"" +
                    $" {OnChangeTime(control, needUpdate)}" +
                    $" id=\"{id}-time\" {disabled} required/>";

            }

            string datetimepicker = $"<div class=\"datetimepicker\" name=\"{control.Name}\" style=\"{GetDateTimePickerDivStyle(control)}\">" +
                date +
                time +
                "</div>";

            return datetimepicker;
        }

        private string GetDateTimePickerDivStyle(DateTimePickerControl control)
        {
            return $"{GetControlPosition(control)} display:inline-flex";
        }        

        private string GetDateTimePickerStyle(DateTimePickerControl control)
        {
            return $"{GetControlFont(control.Font)} {GetBackColor(control.BackColor)}";
        }
    }
}
