using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Printing;

namespace WpfApplication19
{
	public class Printer
	{
        //public int c;
        public static void Print(string s)
        {
            PrintDialog printDialog = new PrintDialog();
            printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
            //string line1 = "x.23   2.42   5.23   23.23   23.42   23.42   23.42\n";
            Run run = new Run(s);

            /*
            Viewbox vb = new Viewbox();
            vb.Stretch = Stretch.Uniform;
            vb.VerticalAlignment = VerticalAlignment.Top;
            vb.HorizontalAlignment = HorizontalAlignment.Left;
            */
            // Wrap it in a TextBlock.
            TextBlock visual = new TextBlock(run);

            //RotateTransform rt = new RotateTransform();
            //rt.Angle = 90;
            //visual.LayoutTransform = rt;
            //visual.RenderTransform = rt; 
            visual.Margin = new Thickness(10);
            // Allow wrapping to fit the page width.
            visual.TextWrapping = TextWrapping.WrapWithOverflow;
            visual.FontSize = Int32.Parse(Utils.getSetting("fontSize"));
            visual.FontFamily = new FontFamily(Utils.getSetting("fontName"));
            visual.FontWeight = FontWeights.Bold;
            //visual.FontStretch = FontStretches.ExtraCondensed;
            // Scale the TextBlock in both dimensions.

            //visual.LayoutTransform = new ScaleTransform(zoom / 100, zoom / 100);

            // Get the size of the page.
            Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
            // Trigger the sizing of the element.                
            visual.Measure(pageSize);
            visual.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
            visual.HorizontalAlignment = HorizontalAlignment.Left;
            visual.VerticalAlignment = VerticalAlignment.Top;
            // Print the element.

            //vb.Child = visual;
            //printDialog.PrintVisual(visual, "A Scaled Drawing");
            printDialog.PrintVisual(visual, "A Scaled Drawing");


        }
        public static void Print(string s, int ncol){
			
			var n = s.Split('\n').Length - 1;
			
			
			int fn;
			if( ncol >= 3 && n <= 5){
				fn = 10;

			}
			else{
				fn = 16 - n;
				if (fn < 10)
					fn = 10;
			}
			
			

			char last = s[s.Length - 1];
			if( last != '\n'){
				s = s + '\n';
			}
			var s3 = s.Split('\n');
			n = s3.Length - 1;
			for (int i = 0; i < n; i += 7)
			{
				string k;
				if (i + 7 > n)
				{
					k = string.Join("\n", s3, i, n - i);
					//Console.WriteLine("*" + k);
					
					
				}
				else
				{
					
					k = String.Join("\n", s3, i, 7);
					//visual.Text = k;
					//Console.WriteLine(k);
				}
				PrintRawLabel(k, fn);
				
			}
			
		}
		static void PrintRawLabel(string s, int fontSize){
			//
			PrintDialog printDialog = new PrintDialog();
			
			printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
			

			Run run = new Run(s);

			// Wrap it in a TextBlock.
			TextBlock visual = new TextBlock(run);
			//visual.FontFamily = new FontFamily("Cordia New");
			visual.Margin = new Thickness(15);
			// Allow wrapping to fit the page width.
			visual.TextWrapping = TextWrapping.Wrap;
			visual.FontStretch = FontStretches.ExtraCondensed;

            visual.FontSize = Int32.Parse(Utils.getSetting("fontSize"));
            visual.FontFamily = new FontFamily(Utils.getSetting("fontName"));
            visual.FontWeight = FontWeights.Bold;
            // Scale the TextBlock in both dimensions.



            Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
			visual.Measure(pageSize);
			visual.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
			
			printDialog.PrintVisual(visual, "InspectionLabel");
		}	
	}
}
