using System.Globalization;
using System.Windows.Forms;
using FastReport.Export;
using FastReport.Export.RichText;
using FastReport.Utils;

namespace FastReport.Forms
{
    /// <summary>
    /// Form for <see cref="RTFExport"/>.
    /// For internal use only.
    /// </summary>
    public partial class RTFExportForm : BaseExportForm
    {
        /// <inheritdoc/>
        public override void Init(ExportBase export)
        {
            base.Init(export);
            RTFExport rtfExport = Export as RTFExport;
            cbWysiwyg.Checked = rtfExport.Wysiwyg;
            cbPrintOptimized.Checked = rtfExport.PrintOptimized;
            cbPageBreaks.Checked = rtfExport.PageBreaks;
            cbbLocalization.Enabled = rtfExport.ExportLocale;
            cbLocalization.Checked = rtfExport.ExportLocale;

            if (rtfExport.Pictures)
                cbbPictures.SelectedIndex = rtfExport.ImageFormat == RTFImageFormat.Metafile ? 1 : (rtfExport.ImageFormat == RTFImageFormat.Jpeg ? 2 : 3);
            else 
                cbbPictures.SelectedIndex = 0;
            if(rtfExport.EmbedRichObject)
                cbbRTF.SelectedIndex = 1;
            else
                cbbRTF.SelectedIndex = 0;

            cbbLocalization.Items.Add(Res.Get("Export,Misc,UseDesigerLocale"));
            foreach (var lang in CultureInfo.GetCultures(CultureTypes.AllCultures))
                cbbLocalization.Items.Add(lang.DisplayName);

            if (rtfExport.Locale != null)
                cbbLocalization.SelectedItem = rtfExport.Locale.DisplayName;
            else
                cbbLocalization.SelectedIndex = 0;
        }

        /// <inheritdoc/>
        protected override void Done()
        {
            base.Done();
            RTFExport rtfExport = Export as RTFExport;
            rtfExport.Wysiwyg = cbWysiwyg.Checked;
            rtfExport.PageBreaks = cbPageBreaks.Checked;
            rtfExport.Pictures = cbbPictures.SelectedIndex > 0;
            rtfExport.ExportLocale = cbLocalization.Checked;

            rtfExport.PrintOptimized = cbPrintOptimized.Checked;

            if (cbbPictures.SelectedIndex == 1)
                rtfExport.ImageFormat = RTFImageFormat.Metafile;
            else if (cbbPictures.SelectedIndex == 2)
                rtfExport.ImageFormat = RTFImageFormat.Jpeg;
            else
                rtfExport.ImageFormat = RTFImageFormat.Png;
            if (cbbRTF.SelectedIndex == 1)
                rtfExport.EmbedRichObject = true;
            else
                rtfExport.EmbedRichObject = false;

            if (cbbLocalization.SelectedIndex == -1 || cbbLocalization.SelectedIndex == 0)
                rtfExport.Locale = null;
            else
                rtfExport.Locale = CultureInfo.GetCultures(CultureTypes.AllCultures)[cbbLocalization.SelectedIndex - 1];
        }

        private void CbLocalization_CheckedChanged(object sender, System.EventArgs e)
        {
            cbbLocalization.Enabled = cbLocalization.Checked;
        }

        /// <inheritdoc/>
        public override void Localize()
        {
            base.Localize();
            MyRes res = new MyRes("Export,RichText");
            Text = res.Get("");
            lblRTF.Text = res.Get("RTFObjectAs");
            cbbRTF.Items[0] = res.Get("Picture");
            cbbRTF.Items[1] = res.Get("EmbeddedRTF");
            res = new MyRes("Export,Misc");            
            gbOptions.Text = res.Get("Options");
            cbWysiwyg.Text = res.Get("Wysiwyg");
            cbPrintOptimized.Text = res.Get("PrintOptimized");
            cbLocalization.Text = res.Get("Localization");
            cbPageBreaks.Text = res.Get("PageBreaks");
            lblPictures.Text = res.Get("Pictures");
            cbbPictures.Items[0] = res.Get("None");
            cbbPictures.Items[1] = res.Get("Metafile");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RTFExportForm"/> class.
        /// </summary>
        public RTFExportForm()
        {
            InitializeComponent();
        }
    }
}

