program Text;
@use "crtc"
@use "input"
@use "text"
@use "system/str"

var
  hw_i: byte=0;
  ch: byte=0;
  matrix: array[8] of byte;
  message: string = ("Print a pointer");
begin
  // Set 160x200 16-color mode
  Crtc::SetMode(0);
  // Set black border
  Crtc::SetBorder(0);

  Text::Print(#message);


  // Write digits from 0 to 10
  for hw_i := 48 to 58 do Text::PutChar(hw_i);
  Input::WaitKey();


  // Print with immediate value
  Text::MoveCursorTo(3, 3);
  Text::Print(string("Print"," a", " string\r\n"));
  Input::WaitKey();

  // Print with a string variable
  Text::Print(#message);
  Input::WaitKey();

  // Ring the bell
  Text::MoveCursorTo(1, 5);
  Text::Print("Hear the bell ");
  Text::PutChar(7);
  Input::WaitKey();
  Text::MoveCursorTo(1, 6);
  Text::Print("See the bell ");
  Text::PutControlChar(7);
  Input::WaitKey();

  // Redefine a char in the default allowed user-definable char
  matrix[0] := 1;
  matrix[1] := 3;
  matrix[2] := 7;
  matrix[3] := 15;
  matrix[4] := 31;
  matrix[5] := 63;
  matrix[6] := 127;
  matrix[7] := 255;

  Text::SetCharMatrix(250, #matrix);
  Text::PutChar(250);

  // Redefine a char not in the default allowed user-definable char
  matrix[0] := 255;
  matrix[1] := 0;
  matrix[2] := 255;
  matrix[3] := 0;
  matrix[4] := 255;
  matrix[5] := 0;
  matrix[6] := 255;
  matrix[7] := 0;

  // Redefine the 'l' to horizontal lines
  Text::SetCharMatrix(108, #matrix);
  Text::PutChar(108);

  // Print Hello, world with the modified 'l' char
  Text::MoveCursorTo(1, 7);
  Text::Print("Hello, world!\r\n");
  Input::WaitKey();

  // Copy 'Hw' from line 5 to line 10
  Text::MoveCursorTo(1, 7);
  ch := Text::GetChar();
  Text::MoveCursorTo(1, 10);
  Text::PutChar(ch);

  Text::MoveCursorTo(8, 7);
  ch := Text::GetChar();
  Text::MoveCursorTo(2, 10);
  Text::PutChar(ch);
  Input::WaitKey();

  // Enable cursor
  Text::EnableCursor(1);
  Input::WaitKey();

  // Disable cursor
  Text::EnableCursor(0);
  Input::WaitKey();

  // One more time with colors
  Text::MoveCursorTo(1, 11);
  Text::Print("GetPen: ");
  Text::PutChar(Text::GetPen()+48);
  Text::MoveCursorTo(1, 12);
  Text::Print("GetPaper: ");
  Text::PutChar(Text::GetPaper()+48);
  Text::MoveCursorTo(1, 13);
  Text::SetPen(3);
  Text::Print("SetPen 3\r\n");
  Text::SetPaper(2);
  Text::Print("SetPaper 2\r\n");
  Text::MoveCursorTo(1, 15);
  Text::Print("GetPen: ");
  Text::PutChar(Text::GetPen()+48);
  Text::MoveCursorTo(1, 16);
  Text::Print("GetPaper: ");
  Text::PutChar(Text::GetPaper()+48);
  Input::WaitKey();

  // Swap colors
  Text::SwapPenPaper();
  Text::MoveCursorTo(1, 17);
  Text::Print("Swap pen and paper");
  Input::WaitKey();
  Text::SwapPenPaper();

  // Transparent/opaque background
  Text::MoveCursorTo(1, 18);
  Text::Print("Background");
  Text::MoveCursorTo(1, 18);
  Text::SetTransparent(1);
  Text::SetPen(5);
  Text::Print("transparent");
  Input::WaitKey();
  Text::MoveCursorTo(1, 18);
  Text::SetTransparent(0);
  Text::Print("opaque");
  Input::WaitKey();

  // We have both kinds of reset
  Text::Reset(0);
  Text::Print("Print after soft reset");
  Input::WaitKey();
  Text::Reset(1);
  Text::Print("Print after hard reset");
  Input::WaitKey();

  // Windows
  for hw_i := 0 to 8 do
  begin
    Text::SelectWindow(hw_i);
    Text::SetWindow(hw_i, 3*hw_i, 8+hw_i, 3*hw_i+2);
    Text::SetPaper(hw_i);
    Text::SetPen(hw_i+8);
    Text::ClearWindow();
    Text::Print("Window #");
  end;
  Input::WaitKey();

  for hw_i := 0 to 8 do
  begin
    Text::SelectWindow(hw_i);
    Text::MoveCursorTo(9, 1);
    Text::PutChar(48+hw_i);
  end;
  Input::WaitKey();
end.
