\ExplSyntaxOn
\usepackage{tikz}



%MARK
\int_new:N\l_pgfgo_mark_sequence_current_int
\int_set:Nn\l_pgfgo_mark_sequence_current_int{1}




\bool_set_false:N\l_pgfgo_mark_color_bool
\bool_set_false:N\l_pgfgo_players_mark_fill_bool
\bool_set_false:N\l_pgfgo_mark_hatched_lines_bool
\bool_set_false:N\l_pgfgo_mark_t_bool

\tl_set:Nn\l_pgfgo_mark_custom_tl{!}


\cs_new:Nn\pgfgo_mark_current:{}


\keys_define:nn { pgfgo / mark }{
	forget						.code:n	=	{\bool_set_false:N\l_pgfgo_goban_remember_mark_bool},
	forget						.groups:n	=	{preset},
	
	remember					.code:n	=	{\bool_set_true:N\l_pgfgo_goban_remember_mark_bool},
	remember					.groups:n	=	{preset},
	
	circle				.code:n			=	{\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_circle:},
	circle				.groups:n		=	{ shape },

	triangle			.code:n			=	{\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_triangle:},
	triangle			.groups:n		=	{ shape },

	square				.code:n			=	{\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_square:},
	square				.groups:n		=	{ shape },
	
	cross					.code:n			=	{\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_cross:},
	cross					.groups:n		=	{ shape },

	filled~square	.code:n			=	{\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_filled_square:},
	filled~square	.groups:n		=	{ shape },

	filled~circle	.code:n			=	{\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_filled_circle:},
	filled~circle	.groups:n		=	{ shape },

	t-circle			.code:n			= {
		\bool_set_true:N\l_pgfgo_mark_t_bool
		\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_t_circle:
		},
	
	t-circle			.groups:n		=	{ shape },

	t-square			.code:n			= {
		\bool_set_true:N\l_pgfgo_mark_t_bool
		\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_t_square:
	},
	
	t-square			.groups:n		=	{ shape },

	hatched~lines	.code:n			=	{
		\bool_set_true:N\l_pgfgo_mark_hatched_lines_bool
		\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_hatched_lines:
	},
	
	hatched~lines		.groups:n		=	{ shape },

	custom					.code:n			= {
																\tl_set:Nn\l_pgfgo_mark_custom_tl{#1}
																\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_custom:
															},
	
	hatched~lines		.groups:n		=	{ shape },

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%-------------------OTRAS KEYS---------------------%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	black				.code:n		=	{
		\bool_set_true:N	\l_pgfgo_players_black_bool
		\bool_set_false:N	\l_pgfgo_players_white_bool
		\bool_set_false:N	\l_pgfgo_players_neutral_bool
	},
	black				.groups:n = { player-id },
	b						.code:n	= {\keys_set:nn{ pgfgo / mark }{black}},
	b						.groups:n = { player-id },
	
	
	white				.code:n		=	{
		\bool_set_true:N	\l_pgfgo_players_white_bool
		\bool_set_false:N	\l_pgfgo_players_black_bool
		\bool_set_false:N	\l_pgfgo_players_neutral_bool
	},
	white				.groups:n = { player-id },
	w						.code:n	= {\keys_set:nn{ pgfgo / mark }{white}},
	w						.groups:n = { player-id },
	
	neutral				.code:n		=	{
		\bool_set_false:N	\l_pgfgo_players_white_bool
		\bool_set_false:N	\l_pgfgo_players_black_bool
		\bool_set_true:N	\l_pgfgo_players_neutral_bool
	},
	neutral				.groups:n = { player-id },
	n						.code:n	= {\keys_set:nn{ pgfgo / mark }{white}},
	n						.groups:n = { player-id },
}











%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------MARCAS-------------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\cs_set:Nn\pgfgo_mark_circle: {
	\pgfsetstrokecolor{\l_pgfgo_players_mark_line_color_tl}
	\pgfsetlinewidth{\l_pgfgo_players_mark_line_width_dim}

	\pgfpathcircle{\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{\l_pgfgo_players_stone_radius_dim * 0.6}

	\pgfusepath{stroke}
}




\cs_set:Nn\pgfgo_mark_triangle: {
	\pgfpathmoveto{\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{0}{\l_pgfgo_players_stone_radius_dim * 0.6}}}
	\pgfpathlineto{\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{-\l_pgfgo_players_stone_radius_dim * 0.866 * 0.6}{-\l_pgfgo_players_stone_radius_dim * 0.5 * 0.6}}}
	\pgfpathlineto{\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{\l_pgfgo_players_stone_radius_dim * 0.866 * 0.6}{-\l_pgfgo_players_stone_radius_dim * 0.5 * 0.6}}}
	\pgfpathclose

	\pgfsetstrokecolor{\l_pgfgo_players_mark_line_color_tl}
	\pgfsetlinewidth{\l_pgfgo_players_mark_line_width_dim}
	\pgfusepath{stroke}
}





\cs_set:Nn\pgfgo_mark_square: {
	\pgfpathrectanglecorners{
		\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}{\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}}}{
		\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{-\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}{-\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}}
	}

	\pgfsetstrokecolor{\l_pgfgo_players_mark_line_color_tl}
	\pgfsetlinewidth{\l_pgfgo_players_mark_line_width_dim}
	\pgfusepath{stroke}
}

\cs_set:Nn\pgfgo_mark_hatched_lines: {
	\pgfpathrectanglecorners{
		\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{\l_pgfgo_players_stone_radius_dim}{\l_pgfgo_players_stone_radius_dim}}}{
		\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{-\l_pgfgo_players_stone_radius_dim}{-\l_pgfgo_players_stone_radius_dim}}
	}

	\bool_if:NTF\l_pgfgo_players_mark_fill_bool{
		\pgfsetfillpattern{north~west~lines}{\l_pgfgo_players_mark_fill_color_tl}}{
		\pgfsetfillpattern{north~west~lines}{gray}}


	\pgfusepath{fill}
}

\cs_set:Nn\pgfgo_mark_filled_square: {
	\pgfpathrectanglecorners{
		\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.5}{\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.5}}}{
		\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{-\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.5}{-\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.5}}
	}

	\bool_if:NTF\l_pgfgo_players_mark_fill_bool{
		\pgfsetfillcolor{\l_pgfgo_players_mark_fill_color_tl}
	}{
		\pgfsetfillcolor{gray}
	}

	\pgfusepath{fill}
}

\cs_set:Nn\pgfgo_mark_filled_circle: {
	\pgfpathcircle{\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{\l_pgfgo_players_stone_radius_dim * 0.5}

	\bool_if:NTF\l_pgfgo_players_mark_fill_bool{
		\pgfsetfillcolor{\l_pgfgo_players_mark_fill_color_tl}
	}{
		\pgfsetfillcolor{gray}
	}

	\pgfusepath{fill}
}

\cs_set:Nn\pgfgo_mark_cross: {
	\pgfpathmoveto{\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}{\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}}}
	\pgfpathlineto{\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{-\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}{-\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}}}
	\pgfpathmoveto{\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{-\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}{\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}}}
	\pgfpathlineto{\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpoint{\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}{-\l_pgfgo_players_stone_radius_dim * 0.7071 * 0.6}}}

	\pgfsetstrokecolor{\l_pgfgo_players_mark_line_color_tl}
	\pgfsetlinewidth{\l_pgfgo_players_mark_line_width_dim}
	\pgfusepath{stroke}
}

\cs_set_eq:NN\pgfgo_mark_current:\pgfgo_mark_cross:



\cs_set:Nn\pgfgo_mark_custom: {
	%Ajustar el tamano de acuerdo a la cantidad de caracteres <--------
	\tl_set:Nn\l_tmpa_tl{0.45em}
	\regex_count:nVN{.}\l_pgfgo_mark_custom_tl\l_tmpa_int
	\int_case:nn{\l_tmpa_int}{
		{1}{\tl_set:Nn\l_tmpa_tl{1em}}
		{2}{\tl_set:Nn\l_tmpa_tl{0.65em}}
		{3}{\tl_set:Nn\l_tmpa_tl{0.45em}}
	}
	%------------------------------------------------------------------

	\pgfsetcolor{\l_pgfgo_players_mark_line_color_tl}
	\pgftext[at = \pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}]{{
			\bfseries\fontsize{\l_tmpa_tl}{0pt}\selectfont\textcolor{\l_pgfgo_players_mark_label_color_tl}{\l_pgfgo_mark_custom_tl}
	}}

	\pgfusepath{stroke}
}

\cs_set:Nn\pgfgo_mark_t_circle: {
	\pgfsetstrokecolor{\l_pgfgo_goban_grid_color_tl}
	\pgfsetlinewidth{\l_pgfgo_goban_grid_line_width_dim}
	\pgfsetfillcolor{\l_pgfgo_players_stone_fill_color_tl}
	\pgfsetstrokeopacity{\l_pgfgo_goban_grid_opacity_tl}

	\pgfpathcircle{\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{\l_pgfgo_players_stone_radius_dim * 0.35}

	\pgfusepath{stroke, fill}
}

\cs_set:Nn\pgfgo_mark_t_square: {
	\pgfsetstrokecolor{\l_pgfgo_goban_grid_color_tl}
	\pgfsetlinewidth{\l_pgfgo_goban_grid_line_width_dim}
	\pgfsetfillcolor{\l_pgfgo_players_stone_fill_color_tl}
	\pgfsetstrokeopacity{\l_pgfgo_goban_grid_opacity_tl}

	\pgfpathrectanglecorners{
		\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpointpolar{45}{\l_pgfgo_players_stone_radius_dim * 0.25 * 1.41}}
		}{
		\pgfpointadd{
			\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{
			\pgfpointpolar{225}{\l_pgfgo_players_stone_radius_dim * 0.25 * 1.41}}
		}

	\pgfusepath{stroke, fill}
}



\cs_set:Nn\pgfgo_mark_sequence: {
	%Ajustar el tamano de acuerdo a la cantidad de caracteres <--------
	\tl_set:Nn\l_tmpa_tl{0.45em}
	\tl_set:Nx\l_tmpb_tl{\pgfgo_players_mark_sequence_format:n{\l_pgfgo_mark_sequence_current_int}}
	\regex_count:nVN{[a-zA-Z\d]}\l_tmpb_tl\l_tmpa_int
	\int_case:nn{\l_tmpa_int}{
		{1}{\tl_set:Nn\l_tmpa_tl{0.7em}}
		{2}{\tl_set:Nn\l_tmpa_tl{0.65em}}
		{3}{\tl_set:Nn\l_tmpa_tl{0.45em}}
	}
	%------------------------------------------------------------------
	\pgfsetcolor{\l_pgfgo_players_mark_label_color_tl}
	\pgftext[at = \pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}]{{
			\bfseries\fontsize{\l_tmpa_tl}{0pt}\selectfont
			\pgfgo_players_mark_sequence_format:n{\l_pgfgo_mark_sequence_current_int}
	}}
	\pgfusepath{stroke}

	\int_gincr:N\l_pgfgo_mark_sequence_current_int
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------MARCA--------------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\cs_set_protected:Npn\pgfgo_mark:nnn #1#2#3 {
	\group_begin:

	\tl_set:Nn\l_pgfgo_parsed_x_coordinate_tl{#2}
	\tl_set:Nn\l_pgfgo_parsed_y_coordinate_tl{#3}
	
	\keys_set_groups:nnn{ pgfgo / mark }{ player-id }{#1}
	\bool_if:NTF \l_pgfgo_players_black_bool 
	{\keys_set_exclude_groups:nnn{ pgfgo / players / black / mark } { external }{#1}}
	{\bool_if:NTF \l_pgfgo_players_white_bool 
		{\keys_set_exclude_groups:nnn{ pgfgo / players / white / mark } { external } {#1}}
		{\keys_set_exclude_groups:nnn{ pgfgo / players / neutral / mark } { external } {#1}}}
	\pgfgo_get_variables_from_current_player:
	\keys_set_groups:nnn {pgfgo / mark} { shape } {#1}
	
	

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%---------------------SHADOW-----------------------%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	\bool_if:NF\l_pgfgo_mark_t_bool{
		\pgfonlayer{pgfgo_layer_mark_shadow}
		\pgfsetfillcolor{\l_pgfgo_goban_border_fill_color_tl}

		\bool_if:NTF\l_pgfgo_mark_hatched_lines_bool{
			\tl_set:Nn\l_pgfgo_mark_shadow_scale{1}
		}{
			\tl_set:Nn\l_pgfgo_mark_shadow_scale{0.8}
		}

		\pgfpathcircle{\pgfpointxy{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}}{\l_pgfgo_players_stone_radius_dim * \l_pgfgo_mark_shadow_scale}

		\pgfusepath{fill}
		\endpgfonlayer
	}

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%---------------------MARCA------------------------%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


	\pgfonlayer{pgfgo_layer_mark}
	\bool_if:NTF \l_pgfgo_players_mark_sequence_bool {
		\pgfgo_mark_sequence:
	}{
		\pgfgo_mark_current:
	}
	
	\endpgfonlayer
	\group_end:
}


\cs_generate_variant:Nn\pgfgo_mark:nnn  {nee}
\cs_new:Nn \pgfgo_mark:nn {\pgfgo_mark:nnn{#1}#2}
\cs_generate_variant:Nn\pgfgo_mark:nn {nV}




\cs_new:Nn \pgfgo_marks_parsed:nn {
	\clist_map_variable:nNn {#2} \variable {\pgfgo_mark:nV{#1}\variable}
}



%\l_pgfgo_mark_sequence_current_int

\cs_set_protected:Npn\pgfgo_marks:nn #1#2 {
	\group_begin:
		\keys_set_groups:nnn{ pgfgo / mark }{ preset }{#1}
		\pgfgo_coordinate_parser_from_pgfgonce_to_items:n{#2}
		%RECORDAR LAS PIEDAS
		\clist_set_from_seq:NN \l_tmpa_clist\l_pgfgo_coordinate_parser_items_from_pgfgonce_seq
		\bool_if:NT \l_pgfgo_goban_remember_mark_bool {
			\seq_gput_right:Ne \l_pgfgo_goban_elements_stack_seq {\exp_not:N\pgfgo_marks_parsed:nn{#1}{\l_tmpa_clist}}
		}
		
		\keys_set_exclude_groups:nnn{ pgfgo / players / neutral / mark } { external }{#1}
		\int_set:NV\l_pgfgo_mark_sequence_current_int\l_pgfgo_neutral_mark_sequence_from_int
		
		 %Devuelve \l_pgfgo_coordinate_parser_items_from_pgfgonce_seq
		
		\seq_map_variable:NNn \l_pgfgo_coordinate_parser_items_from_pgfgonce_seq\variable{
			\pgfgo_mark:nV{#1}\variable
		}
	\group_end:
}



\DeclareDocumentCommand{\marks}{O{}m}{\pgfgo_marks:nn{#1}{#2}}

\let\mark\marks



\ExplSyntaxOff