Debug
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © weak_hand

//@version=5
// https://www.tradingview.com/pine-script-docs/en/v5/writing/Debugging.html
// @description Some debugging functions.
library("Debug", true)

// @function Prints a label on every bar. By default, only the last 50 labels will be shown on the chart. You can increase this amount up to a maximum of 500 by using `max_labels_count = 500` parameter inside `indicator()` or `strategy()`.
// @param txt The text to show on the label.
// @param y_position Y-position (price) to display the label. Optional. Default is `high`.
// @param label_size Possible values: `size.auto`, `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`. Optional. Default value is `size.small`.
// @param label_color Change background color. Optional. Default value is `color.blue`.
// @param txt_color Change text color. Optional. Default value is `color.white`.
// @returns label ID
export label_on_each_bar(string txt = "", float y_position = high, string label_size = size.small, color label_color = color.blue, color txt_color = color.white) =>
	label.new(bar_index, y_position, txt, color = label_color, size = label_size, textcolor = txt_color)

// @function Prints a label at last bar only.
// @param txt The text to show on the label.
// @param y_position Y-position (price) to display the label. Optional. Default is `high`.
// @param label_size Possible values: `size.auto`, `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`. Optional. Default value is `size.large`.
// @param label_color Change background color. Optional. Default value is `color.blue`.
// @param txt_color Change text color. Optional. Default value is `color.white`.
// @param txt_align Text alignment. Optional. Possible values: `text.align_left`, `text.align_center`, `text.align_right`. Default value is `text.align_center`.
// @returns label ID
export label_on_last_bar(string txt = "", float y_position = high, string label_size = size.large, color label_color = color.blue, color txt_color = color.white, string txt_align = text.align_center) =>
	var label_debug = label.new(bar_index, na, txt, xloc = xloc.bar_index, yloc = yloc.price)
	label.set_xy(		label_debug, bar_index, y_position)
	label.set_size(		label_debug, label_size)
	label.set_color(	label_debug, label_color)
	label.set_textalign(label_debug, txt_align)
	label.set_textcolor(label_debug, txt_color)
	label.set_text(		label_debug, txt)
	label_debug

// @function Prints a table to show all symbol information available, including the commands.
// @param table_position Position of the table. Optional. Possible values are: `position.top_left`, `position.top_center`, `position.top_right`, `position.middle_left`, `position.middle_center`, `position.middle_right`, `position.bottom_left`, `position.bottom_center`, `position.bottom_right`. Default value is `position.middle_right`.
// @param table_color The background color of the table. Optional. The default is `color.yellow`.
// @param text_color The color of the text. Optional. The default is `color.black`.
// @param text_size Possible values: `size.auto`, `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`. Optional. Default value is `size.auto`.
// @returns table ID
export table_symbol_informations(string table_position = position.middle_right, color table_color = color.yellow, color text_color = color.black, string text_size = size.auto) =>	
	var table_syminfo = table.new(table_position, 2, 1)
	nl = "\n"
	column_left =
	 "syminfo.basecurrency: "  + nl +
  	 "syminfo.currency: "      + nl +
  	 "syminfo.description: "   + nl +
  	 "syminfo.mintick: "       + nl +
  	 "syminfo.pointvalue: "    + nl +
  	 "syminfo.prefix: "        + nl +
  	 "syminfo.root: "          + nl +
  	 "syminfo.session: "       + nl +
  	 "syminfo.ticker: "        + nl +
  	 "syminfo.tickerid: "      + nl +
  	 "syminfo.timezone: "      + nl +
  	 "syminfo.type: "
	column_right =
	 syminfo.basecurrency              + nl +
  	 syminfo.currency                  + nl +
  	 syminfo.description               + nl +
  	 str.tostring(syminfo.mintick)     + nl +
  	 str.tostring(syminfo.pointvalue)  + nl +
  	 syminfo.prefix                    + nl +
  	 syminfo.root                      + nl +
  	 syminfo.session                   + nl +
  	 syminfo.ticker                    + nl +
  	 syminfo.tickerid                  + nl +
  	 syminfo.timezone                  + nl +
  	 syminfo.type
	if barstate.isfirst
    	table.cell(table_syminfo, 0, 0, column_left , bgcolor = table_color, text_color = text_color, text_halign = text.align_right, text_size = text_size)
    	table.cell(table_syminfo, 1, 0, column_right, bgcolor = table_color, text_color = text_color, text_halign = text.align_left , text_size = text_size)
	table_syminfo

// this is a all in one function for the following four functions (table_array_boolean, table_array_float, table_array_integer, and table_array_string)
table_array(array_for_table, columns, rows, position, bgcolor, txt_color, txt_size) =>
	int	row_count = 0
	if rows == 0
		row_count := math.ceil(array_for_table.size() / columns)
	else
		row_count := rows

	if barstate.islast

		my_table = table.new(position, columns, row_count, bgcolor)

		int count_columns = 0
		int count_rows    = -1
		for counter = 0 to array_for_table.size() - 1
			count_rows += 1

			if count_rows > row_count - 1
				count_columns += 1
				count_rows 	  := 0

			if count_columns > columns - 1
				continue
			else
				my_table.cell(count_columns, count_rows, str.format("{0}: {1}", counter, array_for_table.get(counter)), text_color = txt_color, text_size = txt_size)

		my_table

// @function Prints a table to show boolean values of an array.
// @param array_boolean The array to display on the table. The type of the array has to use boolean values!
// @param columns The number of columns to use. Optional. Default is one.
// @param rows The number of rows to show the values. Optional. Default is `array_float.size()`.
// @param position Position of the table. Optional. Possible values are: `position.top_left`, `position.top_center`, `position.top_right`, `position.middle_left`, `position.middle_center`, `position.middle_right`, `position.bottom_left`, `position.bottom_center`, `position.bottom_right`. Default value is `position.middle_center`.
// @param bgcolor The background color of the table. Optional. There is no color by default.
// @param txt_color The color of the text. Optional. Default value is `color.blue`.
// @param txt_size The size of the Text. Possible values: `size.auto`, `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`. Optional. Default value is `size.auto`.
// @returns table ID
export table_array_boolean(bool[] array_boolean, int columns = 1, int rows = 0, string position = position.middle_center, color bgcolor = na, color txt_color = color.blue, string txt_size = size.auto) =>
	table_array(array_boolean, columns, rows, position, bgcolor, txt_color, txt_size)

// @function Prints a table to show float values of an array.
// @param array_float The array to display on the table. The type of the array has to use float values!
// @param columns The number of columns to use. Optional. Default is one.
// @param rows The number of rows to show the values. Optional. Default is `array_float.size()`.
// @param position Position of the table. Optional. Possible values are: `position.top_left`, `position.top_center`, `position.top_right`, `position.middle_left`, `position.middle_center`, `position.middle_right`, `position.bottom_left`, `position.bottom_center`, `position.bottom_right`. Default value is `position.middle_center`.
// @param bgcolor The background color of the table. Optional. There is no color by default.
// @param txt_color The color of the text. Optional. Default value is `color.blue`.
// @param txt_size The size of the Text. Possible values: `size.auto`, `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`. Optional. Default value is `size.auto`.
// @returns table ID
export table_array_float(float[] array_float, int columns = 1, int rows = 0, string position = position.middle_center, color bgcolor = na, color txt_color = color.blue, string txt_size = size.auto) =>
	table_array(array_float, columns, rows, position, bgcolor, txt_color, txt_size)

// @function Prints a table to show int values of an array.
// @param array_int The array to display on the table. The type of the array has to use int values!
// @param columns The number of columns to use. Optional. Default is one.
// @param rows The number of rows to show the values. Optional. Default is `array_float.size()`.
// @param position Position of the table. Optional. Possible values are: `position.top_left`, `position.top_center`, `position.top_right`, `position.middle_left`, `position.middle_center`, `position.middle_right`, `position.bottom_left`, `position.bottom_center`, `position.bottom_right`. Default value is `position.middle_center`.
// @param bgcolor The background color of the table. Optional. There is no color by default.
// @param txt_color The color of the text. Optional. Default value is `color.blue`.
// @param txt_size The size of the Text. Possible values: `size.auto`, `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`. Optional. Default value is `size.auto`.
// @returns table ID
export table_array_integer(int[] array_int, int columns = 1, int rows = 0, string position = position.middle_center, color bgcolor = na, color txt_color = color.blue, string txt_size = size.auto) =>
	table_array(array_int, columns, rows, position, bgcolor, txt_color, txt_size)

// @function Prints a table to show string values of an array.
// @param array_string The array to display on the table. The type of the array has to use string values!
// @param columns The number of columns to use. Optional. Default is one.
// @param rows The number of rows to show the values. Optional. Default is `array_float.size()`.
// @param position Position of the table. Optional. Possible values are: `position.top_left`, `position.top_center`, `position.top_right`, `position.middle_left`, `position.middle_center`, `position.middle_right`, `position.bottom_left`, `position.bottom_center`, `position.bottom_right`. Default value is `position.middle_center`.
// @param bgcolor The background color of the table. Optional. There is no color by default.
// @param txt_color The color of the text. Optional. Default value is `color.blue`.
// @param txt_size The size of the Text. Possible values: `size.auto`, `size.tiny`, `size.small`, `size.normal`, `size.large`, `size.huge`. Optional. Default value is `size.auto`.
// @returns table ID
export table_array_string(string[] array_string, int columns = 1, int rows = 0, string position = position.middle_center, color bgcolor = na, color txt_color = color.blue, string txt_size = size.auto) =>
	table_array(array_string, columns, rows, position, bgcolor, txt_color, txt_size)


table_matrix(matrix_for_table, from_column, to_column, from_row, to_row, position, bgcolor, txt_color, txt_size, is_string = false) =>
	int column_count = na
	if na(to_column)
		column_count := matrix_for_table.columns()
	else
		column_count := to_column

	int	row_count = na
	if na(to_row)
		row_count := matrix_for_table.rows()
	else
		row_count := to_row

	submatrix_for_table = matrix_for_table.submatrix(from_row, row_count, from_column, column_count)
	my_table = table.new(position, submatrix_for_table.columns(), submatrix_for_table.rows(), bgcolor)

	for columns_counter = 0 to submatrix_for_table.columns() - 1
		for rows_counter = 0 to submatrix_for_table.rows() - 1
			if is_string
				my_table.cell(columns_counter, rows_counter, str.tostring(submatrix_for_table.get(rows_counter, columns_counter)), text_color = txt_color, text_size = txt_size)
			else
				my_table.cell(columns_counter, rows_counter, str.tostring(submatrix_for_table.get(rows_counter, columns_counter)), text_color = txt_color, text_size = txt_size)

	my_table

//	log.info("\n is_string: {0}", str.tostring(is_string))

export table_matrix_boolean(matrix<bool> matrix_boolean, int from_column = 0, int to_column = na, int from_row = 0, int to_row = na, string position = position.middle_center, color bgcolor = na, color txt_color = color.blue, string txt_size = size.auto) =>
	table_matrix(matrix_boolean, from_column, to_column, from_row, to_row, position, bgcolor, txt_color, txt_size, false)

export table_matrix_float(matrix<float> matrix_float, int from_column = 0, int to_column = na, int from_row = 0, int to_row = na, string position = position.middle_center, color bgcolor = na, color txt_color = color.blue, string txt_size = size.auto) =>
	table_matrix(matrix_float, from_column, to_column, from_row, to_row, position, bgcolor, txt_color, txt_size, false)

export table_matrix_integer(matrix<int> matrix_int, int from_column = 0, int to_column = na, int from_row = 0, int to_row = na, string position = position.middle_center, color bgcolor = na, color txt_color = color.blue, string txt_size = size.auto) =>
	table_matrix(matrix_int, from_column, to_column, from_row, to_row, position, bgcolor, txt_color, txt_size, false)

export table_matrix_string(matrix<string> matrix_string, int from_column = 0, int to_column = na, int from_row = 0, int to_row = na, string position = position.middle_center, color bgcolor = na, color txt_color = color.blue, string txt_size = size.auto) =>
	table_matrix(matrix_string, from_column, to_column, from_row, to_row, position, bgcolor, txt_color, txt_size, false)

// ----------------------------------------------}
// OUTPUTS
// ----------------------------------------------{

//label_on_each_bar(str.tostring(bar_index), high)
//label_on_last_bar("Hello\nWorld!")
//table_symbol_informations()

//var my_array = array.from(.815, .815, .815)
//table_array_float(my_array, 4)

//var my_array = array.from(1, 2, 3)
//table_array_integer(my_array, 4, 2)

//var my_array = array.from("Hello")
//var table my_id = na
//if barstate.islast
//	if my_array.size() == 1 and barstate.isconfirmed
//		my_array.push("World")
//		my_id.delete()
//	else if my_array.size() == 2 and barstate.isconfirmed
//		my_array.push("!")
//		my_id.delete()
//	my_id := table_array_string(my_array)

//var my_matrix = matrix.new<int>(3, 3, 4711)
//var table my_id = na
//if barstate.islast
//	if my_matrix.columns() < 10 and barstate.isconfirmed
//		my_id.delete()
//		my_matrix.add_col()
//		my_matrix.fill(666, 0, my_matrix.rows(), my_matrix.columns() - 1, my_matrix.columns())
//	my_id := table_matrix_integer(my_matrix)
