/* ITEM-*		=	"ITEM-"		"*" */
/* ITEM*		=	"ITEM"		"*" */
/* ITEM-????	=	"ITEM-"		"????" */
/* ITEM?????	=	"ITEM"		"????" */

/* * */

/* *.*		=	"*"	"."	"*" */
/* ??-*		=	"??"	"-"	"*" */
/* AB??-??cd	=	"AB"	"??"	"-"	"??"	"cd" */

/* 123456789 */
/* AB??-*DEF */


-- Stored Procedure
--    Author:           Rohith
--    Create date:      28/10/2021
--    Description:      To generate segements based on the MASK     
--  Change history
--      20/12/2021  - Rohith - stabilised script
--      24/02/2022  - Rohith - Added new logic to handle MAJOR MASKS


CREATE OR ALTER PROCEDURE MIG_GENERATE_MASK_SEGEMENTS_Sp (	@MASK_ID NVARCHAR(15),
													@string_value NVARCHAR(MAX),
													@PATTERN NVARCHAR(MAX) )
AS
BEGIN
	

DECLARE @cur_char CHAR(1),
		@IS_SEQ_OR_CONST BIT,  -- 1 SEQ, 0 CONST
		@len INT,
		@str_len INT,
		@part_string NVARCHAR(MAX),
		@part_pattern NVARCHAR(MAX)

		DECLARE @result_set TABLE(MASK_ID NVARCHAR(15), SEGMENT NVARCHAR(MAX), IS_SEQ_OR_CONST BIT, PATTERN NVARCHAR(MAX)) 
		
		DECLARE @STR_TO_CHARS TABLE(INDX INT, CHAR_VAL NCHAR(1)) 

		--SET @MASK_ID = 'ITEM-1'
		--SET @string_value =  'AB??-??cd' -- 'ITEM-????' --
		SET @IS_SEQ_OR_CONST = 0
		SET @len = 0
		SET @str_len = LEN(@string_value)
		SET @part_string = NULL

		-- -----------------------------
		DECLARE @cur_idx INT, @start_idx INT, @end_idx INT
		SET @cur_idx = 0 -- START POSITION
		-- -----------------------------

		INSERT INTO @STR_TO_CHARS
		SELECT * FROM [MIG_STRING_TO_CHARS_Fn](@string_value)
		--SELECT * FROM @STR_TO_CHARS

		WHILE @len < @str_len 
		BEGIN
			
			SET @len = @len +1
			SELECT @cur_char = CHAR_VAL FROM @STR_TO_CHARS WHERE INDX = @len

			IF @IS_SEQ_OR_CONST = 0   -- SEQ
			BEGIN
				IF (@cur_char <> '?' AND @cur_char <> '*')
					BEGIN
						SET @part_string = CONCAT(@part_string, @cur_char)
						CONTINUE;
					END
				ELSE
					BEGIN
						IF(LEN(@part_string) > 0)
						BEGIN	
								INSERT INTO @result_set (MASK_ID, SEGMENT, IS_SEQ_OR_CONST, PATTERN) 
								VALUES(@MASK_ID, @part_string, @IS_SEQ_OR_CONST, NULL)
						END
					
						SET @part_string = NULL	
						SET @IS_SEQ_OR_CONST = 1
						SET @len = @len - 1

						CONTINUE;
					END
			END
	
			ELSE IF @IS_SEQ_OR_CONST = 1  -- CONST 
			BEGIN
				IF (@cur_char = '?' OR @cur_char = '*')
					BEGIN
						SET @part_string = CONCAT(@part_string, @cur_char)
						CONTINUE;
					END
				ELSE
					BEGIN
						IF(LEN(@part_string) > 0)
						BEGIN	
								SET @start_idx = @cur_idx

								SET @end_idx = CHARINDEX(']', @PATTERN, @start_idx)
								IF @end_idx > 0
									BEGIN  
										-- INCASE of MAJOR MASK we need this PATTERN split
										SET @part_pattern = SUBSTRING(@PATTERN, @start_idx, (@end_idx + 1) - @start_idx)
										SET @cur_idx = @end_idx +1
									END
								ELSE
									BEGIN
										SET @cur_idx = 0
										SET @part_pattern = NULL
									END

								INSERT INTO @result_set (MASK_ID, SEGMENT, IS_SEQ_OR_CONST, PATTERN) 
								VALUES(@MASK_ID, @part_string, @IS_SEQ_OR_CONST, @part_pattern)
						END
					
						SET @part_string = NULL	
						SET @part_pattern = NULL
						SET @IS_SEQ_OR_CONST = 0
						SET @len = @len - 1

						CONTINUE;
					END
			END

		
		END

		-- To INSERT LAST OCCURANCE PART STRING
		IF(LEN(@part_string) > 0)
				BEGIN	
					SET @start_idx = @cur_idx

					SET @end_idx = CHARINDEX(']', @PATTERN, @start_idx)
					IF @end_idx > 0
						BEGIN  
							SET @part_pattern = SUBSTRING(@PATTERN, @start_idx, (@end_idx +1) - @start_idx)
							SET @cur_idx = @end_idx +1
						END
					ELSE
						BEGIN
							SET @cur_idx = 0
						END

					INSERT INTO @result_set (MASK_ID, SEGMENT, IS_SEQ_OR_CONST, PATTERN) 
					VALUES(@MASK_ID, @part_string, @IS_SEQ_OR_CONST, IIF(LEN(@part_pattern) > 0, @part_pattern, NULL) )
				END

		SELECT * FROM @result_set
		WHERE SEGMENT NOT IN ('[', '][', ']')  -- MAJOR MASK semnario - ignore square brakets.

END

