\ExplSyntaxOn



%STONE
\bool_set_false:N\l_pgfgo_stone_mark_bool

%MOVE
\int_new:N\l_pgfgo_move_from_int
\int_set:Nn\l_pgfgo_move_from_int{1}
\int_new:N\l_pgfgo_move_current_int
\int_set:Nn\l_pgfgo_move_current_int{1}
%MIDE LOS INTERCALOS ENTRE LOS MOVIMIENTOS
\bool_set_true:N\l_pgfgo_move_player_black_bool
\bool_set_false:N\l_pgfgo_move_player_white_bool


\tl_new:N \l_pgfgo_stones_mark_tl


\bool_set_false:N\l_pgfgo_prisoner_bool


\keys_define:nn { pgfgo / stone }{
	forget						.code:n	=	{\bool_set_false:N\l_pgfgo_goban_remember_stone_bool},
	forget						.groups:n	=	{preset},

	remember					.code:n	=	{\bool_set_true:N\l_pgfgo_goban_remember_stone_bool},
	remember					.groups:n	=	{preset},

	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 / stone }{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 / stone }{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 / stone }{white}},
	n						.groups:n = { player-id },
	
	mark								.code:n = {
			\pgfgo_mark:nee{#1}{\l_pgfgo_parsed_x_coordinate_tl}{\l_pgfgo_parsed_y_coordinate_tl}
	},
	
	mark		.groups:n = {mark}
}





\keys_define:nn { pgfgo / move} {
	forget						.code:n	=	{\bool_set_false:N\l_pgfgo_goban_remember_move_bool},
	forget						.groups:n	=	{preset, local},

	remember					.code:n	=	{
		\bool_set_true:N\l_pgfgo_goban_remember_move_bool},
	remember					.groups:n	=	{preset, local},


	black			.code:n			= {
		\bool_set_true:N\l_pgfgo_players_black_bool
		\bool_set_false:N\l_pgfgo_players_white_bool
	},
	black			.groups:n		= {player-id},
	b						.code:n	= {\keys_set:nn{ pgfgo / move }{black}},
	b						.groups:n = { player-id, local },


	white			.code:n			= {
		\bool_set_false:N\l_pgfgo_players_black_bool
		\bool_set_true:N\l_pgfgo_players_white_bool
	},
	white			.groups:n		= { player-id },
	w						.code:n	= {\keys_set:nn{ pgfgo / move }{white}},
	w						.groups:n = { player-id, local },


	from				.code:n			=	{
		\int_set:Nn\l_pgfgo_move_from_int{#1}
		\int_set:Nn\l_pgfgo_move_current_int{\int_use:N\l_pgfgo_move_from_int}
	},
	from				.groups:n		= {first stone, preset, local},
}

\keys_set:nn { pgfgo / move} {black}






%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------PIEDRA-------------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\cs_new:Nn \pgfgo_stones_shine_default: {
	\pgfscope
	\pgfsetfillopacity{\fp_use:N\l_pgfgo_players_stone_drop_shine_opacity_fp}
	\pgftransformrotate{-45}
	\pgftransformrotate{\l_pgfgo_players_stone_drop_shine_angle_fp}
	\pgfsetfillcolor{wainshade}
	\fp_set:Nn\l_tmpa_fp{0.8}
	\pgfpathmoveto{\pgfpointpolar{135}{\fp_use:N\l_tmpa_fp * \l_pgfgo_players_stone_radius_dim * \fp_to_decimal:N\l_pgfgo_players_stone_scale_fp}}
	\pgfpatharc{135}{190}{\fp_use:N\l_tmpa_fp * \l_pgfgo_players_stone_radius_dim * \fp_to_decimal:N\l_pgfgo_players_stone_scale_fp}
	\pgfpathquadraticcurveto{\pgfpointpolar{135}{\fp_use:N\l_tmpa_fp * \l_pgfgo_players_stone_radius_dim * \fp_to_decimal:N\l_pgfgo_players_stone_scale_fp}}{\pgfpointpolar{80}{\fp_use:N\l_tmpa_fp * \l_pgfgo_players_stone_radius_dim * \fp_to_decimal:N\l_pgfgo_players_stone_scale_fp}}
	\pgfpatharc{80}{135}{\fp_use:N\l_tmpa_fp * \l_pgfgo_players_stone_radius_dim * \fp_to_decimal:N\l_pgfgo_players_stone_scale_fp}
	\pgfpathclose
	\pgfusepath{fill}
	
	\pgfsetfillcolor{wainshade}
	\pgftransformshift{\pgfpointpolar{-45}{\fp_use:N\l_tmpa_fp * \l_pgfgo_players_stone_radius_dim * \fp_to_decimal:N\l_pgfgo_players_stone_scale_fp}}
	\pgftransformrotate{45}
	\pgfpathellipse{\pgfpointorigin}{\pgfpoint{0.04cm}{0cm}}{\pgfpoint{0cm}{0.01cm}}
	\pgfusepath{fill}
	\endpgfscope
}






\cs_set:Nn\pgfgo_stone_set_render_options:{
	\pgfonlayer{pgfgo_layer_stone}
	\pgfsetfillcolor{\l_pgfgo_players_stone_fill_color_tl}
	\pgfsetfillopacity{\fp_use:N\l_pgfgo_players_stone_fill_opacity_fp}
	\pgfsetstrokecolor{\l_pgfgo_players_stone_line_color_tl}
	\pgfsetstrokeopacity{\fp_use:N\l_pgfgo_players_stone_line_opacity_fp}
	\pgfsetlinewidth{\l_pgfgo_players_stone_line_width_dim * \fp_use:N \l_pgfgo_players_stone_scale_fp}
	\endpgfonlayer
}


\cs_set:Nn\pgfgo_stone_default_shape:nn{
	%SOMBRA
	\bool_if:NT\l_pgfgo_players_stone_drop_shadow_bool{
		\pgfscope
		\pgfsetfillopacity{\fp_use:N\l_pgfgo_players_stone_drop_shadow_opacity_fp}
		\pgfsetfillcolor{black}
		\pgftransformshift{\pgfpointpolar{\fp_use:N\l_pgfgo_players_stone_drop_shadow_angle_fp}{\dim_use:N\l_pgfgo_players_stone_drop_shadow_offset_dim}}
		\pgfpathcircle{
			\pgfpointxy{#1}{#2}}{
			\l_pgfgo_players_stone_radius_dim * \fp_to_decimal:N\l_pgfgo_players_stone_scale_fp
		}
		\pgfusepath{fill}
		\pgftransformreset
		\endpgfscope
	}
	%PIEDRA
	\pgfpathcircle{
		\pgfpointxy{#1}{#2}}{
		\l_pgfgo_players_stone_radius_dim * \fp_to_decimal:N\l_pgfgo_players_stone_scale_fp
	}
	\pgfusepath{\bool_if:NT\l_pgfgo_players_stone_line_bool{stroke},\bool_if:NT\l_pgfgo_players_stone_fill_bool{fill}}
	%SHINE
	\bool_if:NT \l_pgfgo_players_stone_drop_shine_bool {
		\pgfscope
			\pgftransformshift{\pgfpointxy{#1}{#2}}
			\pgfgo_stones_shine_default:
			\pgftransformreset
		\endpgfscope
	}
}




\cs_set:Nn\pgfgo_stone_render_shape:nn{
	\pgfonlayer{pgfgo_layer_stone}
	\pgfgo_stone_default_shape:nn{#1}{#2}
	\endpgfonlayer
}






\cs_set_protected:Npn\pgfgo_stone:nnn #1#2#3 {
	\group_begin:
	\pgfgo_parse_coordinate:n {#2}
		
	\keys_set_groups:nnn{ pgfgo / stone }{ player-id }{#1}
	\bool_if:NTF \l_pgfgo_players_black_bool 
		{\keys_set_exclude_groups:nnn{ pgfgo / players / black / stone } { external } {#1}}
		{\bool_if:NTF \l_pgfgo_players_white_bool 
			{\keys_set_exclude_groups:nnn{ pgfgo / players / white / stone } { external } {#1}}
			{\keys_set_exclude_groups:nnn{ pgfgo / players / neutral / stone } { external } {#1}}}
	\keys_set_groups:nnn{ pgfgo / stone }{ at-end }{#1}
	\pgfgo_get_variables_from_current_player:
	
	
	\pgfgo_stone_set_render_options:

	\pgfgo_stone_render_shape:nn{#2}{#3}
	
	\tl_set:Nn\l_pgfgo_parsed_x_coordinate_tl{#2}
	\tl_set:Nn\l_pgfgo_parsed_y_coordinate_tl{#3}
	
	%-----MARCAS-------------------------

		\pgfscope
		\keys_set_groups:nnn {pgfgo / stone} {mark} {#1}
		\endpgfscope

	\group_end:
}
\cs_generate_variant:Nn \pgfgo_stone:nnn {Vnn}

\cs_new:Nn \pgfgo_stone:nn {\pgfgo_stone:nnn{#1}#2}
\cs_generate_variant:Nn\pgfgo_stone:nn {nV}
\cs_generate_variant:Nn\pgfgo_stone:nn {nf}



\DeclareDocumentCommand{\stone}{O{}m}{
	\keys_set_groups:nnn{ pgfgo / stone }{ preset }{#1}
	\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
	\regex_extract_all:NnN\l_pgfgo_coordinate_parser_subset_AN_regex{#2}\l_tmpa_seq
	\seq_if_empty:NF \l_tmpa_seq {
		\pgfgo_coordinate_parser_AN:ff{\seq_item:Nn \l_tmpa_seq {2}}{\seq_item:Nn \l_tmpa_seq {3}} %Devuelve \l_pgfgo_coordinate_parser_inmediate_seq
		%RECORDAR LAS PIEDAS
		\bool_if:NT \l_pgfgo_goban_remember_stone_bool {
			\seq_gput_right:Ne \l_pgfgo_goban_elements_stack_seq {
				\exp_not:N\pgfgo_stone:nf{#1}{\seq_item:Nn \l_pgfgo_coordinate_parser_inmediate_seq {1}}}
		}
		
		\pgfgo_stone:nf{#1}{\seq_item:Nn \l_pgfgo_coordinate_parser_inmediate_seq {1}}
	}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------PIEDRAS------------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\cs_new:Nn \pgfgo_stones_parsed:nn {
	\clist_map_variable:nNn {#2} \variable {\pgfgo_stone:nV{#1}\variable}
}








\cs_set:Npn\pgfgo_stones:nn #1#2 {
	\group_begin:
	
	\pgfgo_coordinate_parser_from_pgfgonce_to_items:n{#2} %Devuelve \l_pgfgo_coordinate_parser_items_from_pgfgonce_seq
	
	\keys_set_groups:nnn{ pgfgo / stone }{ preset }{#1}
	%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_stone_bool {
		\seq_gput_right:Ne \l_pgfgo_goban_elements_stack_seq {\exp_not:N\pgfgo_stones_parsed:nn{#1}{\l_tmpa_clist}}
	}
	
	\seq_map_variable:NNn \l_pgfgo_coordinate_parser_items_from_pgfgonce_seq\variable {
		%DIBUJAR LAS PIEDRAS
		\pgfgo_stone:nV{#1}\variable
	}
	\group_end:
}

\cs_generate_variant:Nn\pgfgo_stones:nn{nx}








\NewDocumentCommand{\stones}{O{}m}{\pgfgo_stones:nn {#1}{#2}}

%\let\stone\stones


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------PRISIONEROS--------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NewDocumentCommand{\prisoners}{O{}m}{\stones[prisoner, #1]{#2}}
\NewDocumentCommand{\prisoner}{O{}m}{\stone[prisoner, #1]{#2}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-------------------------------------------MOVE------------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



\cs_set_protected:Npn\pgfgo_move:nnnn #1#2#3#4 {
	\group_begin:
	\pgfgo_get_variables_from_current_player:

	\tl_set:Nn\l_pgfgo_move_x_int{#3}
	\tl_set:Nn\l_pgfgo_move_y_int{#4}


	%CALCULA Y ALTERNA LOS COLORES
	\bool_if:NT\l_pgfgo_players_black_bool{
		\keys_set_exclude_groups:nnn {pgfgo / players / black / move} {external} {#2}
		\pgfgo_get_variables_from_current_player:
		\pgfgo_stone:nnn{black}{#3}{#4}
	}

	\bool_if:NT\l_pgfgo_players_white_bool{
		\keys_set_exclude_groups:nnn {pgfgo / players / white / move} {external} {#2}
		\pgfgo_get_variables_from_current_player:
		\pgfgo_stone:nnn{white}{#3}{#4}
	}

	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%----------------------LABEL-----------------------%
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


	\tl_set:Ne \l_tmpb_tl {\pgfgo_players_move_label_format:n{\l_pgfgo_move_current_int}}
	\tl_set:Nn\l_tmpa_tl{0.45em}
	%Ajustar el tamano de acuerdo a la cantidad de caracteres <--------
	\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{1em}}
		{2}{\tl_set:Nn\l_tmpa_tl{0.65em}}
		{3}{\tl_set:Nn\l_tmpa_tl{0.45em}}
	}
	%------------------------------------------------------------------
	
	\IfBooleanT{#1}{
		\pgfscope
		%TOMA EL COLOR CALCULADO ARRIBA
		\pgfsetcolor{\l_pgfgo_players_move_label_color_tl}
		\pgftext[at=\pgfpointxy{\l_pgfgo_move_x_int}{\l_pgfgo_move_y_int}]{
			\bfseries\fontsize{ \l_tmpa_tl }{0pt}\selectfont
			\pgfgo_players_move_label_format:n{\l_pgfgo_move_current_int}
		}
		\pgfusepath{stroke}
		\endpgfscope
	}

	\int_gincr:N\l_pgfgo_move_current_int

	\group_end:
	\bool_gset_inverse:N\l_pgfgo_players_black_bool
	\bool_gset_inverse:N\l_pgfgo_players_white_bool
}


\cs_new:Nn\pgfgo_move:nnn{\pgfgo_move:nnnn{#1}{#2}#3}
\cs_generate_variant:Nn \pgfgo_move:nnn {nnV}

\cs_generate_variant:Nn\tl_count:n {x}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-------------------------------------------MOVES-----------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




\cs_new:Nn \pgfgo_move_parsed:nnn {
	\keys_set_groups:nnn { pgfgo / move} {player-id, first stone} {#2}
	\clist_map_variable:nNn {#3} \variable {\pgfgo_move:nnV{#1}{#2}\variable}
}





\NewDocumentCommand{\moves}{sO{}m}{
	\group_begin:
	\pgfgo_coordinate_parser_from_pgfgonce_to_items:n{#3} %Devuelve \l_pgfgo_coordinate_parser_items_from_group_seq
	%Eliminar la key from para que no la usen las marcas posteriores.
	
	
	\keys_set_groups:nnn { pgfgo / move} {player-id, first stone} {#2}
	\keys_set_groups:nnn { pgfgo / players / neutral / move} { label } {#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_move_bool {
		\bool_if:NTF \l_pgfgo_goban_remember_move_label_bool{
			\seq_gput_right:Ne \l_pgfgo_goban_elements_stack_seq {
				\exp_not:N\pgfgo_move_parsed:nnn{\exp_not:N \BooleanTrue}{from = \int_use:N\l_pgfgo_move_current_int, #2}{\l_tmpa_clist}
			}
		} {
			\seq_gput_right:Ne \l_pgfgo_goban_elements_stack_seq {
				\exp_not:N\pgfgo_move_parsed:nnn{\exp_not:N \BooleanFalse}{from = \int_use:N\l_pgfgo_move_current_int, #2}{\l_tmpa_clist}
			}
		}
		\seq_log:N \l_pgfgo_goban_elements_stack_seq
	}
	
	%-------------------------------------------------------------------------
	
	
	\seq_map_variable:NNn \l_pgfgo_coordinate_parser_items_from_pgfgonce_seq\variable{
		%PIEDRAS RESTANTES
		\pgfgo_move:nnV {#1}{#2}\variable
	}
	\group_end:
}

\let\move\moves

\ExplSyntaxOff



