 
USE parts;

DROP PROCEDURE IF EXISTS parts.p_dog_calc_button_icon;

DELIMITER //
CREATE PROCEDURE parts.p_dog_calc_button_icon (
      IN a_guid BINARY(36)
    , IN a_id_user INT
    , IN a_get_all_file_type BIT
	, IN a_get_inactive_file_type BIT
	, IN a_ids_file_type TEXT
    , IN a_names_file_type TEXT
    , IN a_get_all_image BIT
	, IN a_get_inactive_image BIT
	, IN a_ids_image TEXT
    , IN a_names_image TEXT
    , IN a_get_all_button_icon BIT
	, IN a_get_inactive_button_icon BIT
	, IN a_ids_button_icon TEXT
    , IN a_names_button_icon TEXT
    , IN a_notes_button_icon TEXT
    , IN a_require_all_id_search_filters_met BIT
    , IN a_require_any_id_search_filters_met BIT
    , IN a_require_all_non_id_search_filters_met BIT
    , IN a_require_any_non_id_search_filters_met BIT
    , IN a_show_errors BIT
    , IN a_debug BIT
)
BEGIN
    DECLARE v_can_view BIT;
    DECLARE v_code_type_error_bad_data VARCHAR(100);
    DECLARE v_code_type_error_no_permission VARCHAR(100);
    DECLARE v_has_filter_image_id BIT;
    DECLARE v_has_filter_image_name BIT;
    DECLARE v_has_filter_button_icon_id BIT;
    DECLARE v_has_filter_button_icon_name BIT;
    DECLARE v_has_filter_button_icon_notes BIT;
    DECLARE v_id_access_level_view INT;
    DECLARE v_id_minimum INT;
    DECLARE v_id_permission_dog_view INT;
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_id_type_error_no_permission INT;
    DECLARE v_time_start TIMESTAMP(6);
    
    DECLARE exit handler for SQLEXCEPTION
    BEGIN
        GET DIAGNOSTICS CONDITION 1
            @sqlstate = RETURNED_SQLSTATE
            , @errno = MYSQL_ERRNO
            , @text = MESSAGE_TEXT
		;
        
        ROLLBACK;
        
		CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error_Calc_Button_Icon (
			  id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
			, id_type INT NULL
			, code VARCHAR(250) NOT NULL
			, msg TEXT NOT NULL
		);

        INSERT INTO tmp_Msg_Error_Calc_Button_Icon (
			  id_type
            , code
            , msg
		)
        SELECT 
			  MET.id_type
            , @errno
            , @text
		FROM parts.CORE_Msg_Error_Type MET
        WHERE MET.code = 'MYSQL_ERROR'
		;
        
        SELECT 
            t_ERROR.id_error
            , t_ERROR.id_type
            , t_ERROR.code
            , ERROR_TYPE.name
            , ERROR_TYPE.description
            , ERROR_TYPE.is_breaking_error
            , ERROR_TYPE.background_colour
            , ERROR_TYPE.text_colour
            , t_ERROR.msg
        FROM tmp_Msg_Error_Calc_Button_Icon t_ERROR
        INNER JOIN parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;
        
		DROP TABLE IF EXISTS tmp_Msg_Error_Calc_Button_Icon;
    END;

    SET v_time_start := CURRENT_TIMESTAMP(6);
    SET v_code_type_error_bad_data := 'BAD_DATA';
    SET v_code_type_error_no_permission := 'NO_PERMISSION';
    SET v_id_type_error_bad_data := (SELECT ERROR_TYPE.id_type FROM parts.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_bad_data LIMIT 1);
    SET v_id_type_error_no_permission := (SELECT ERROR_TYPE.id_type FROM parts.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_no_permission LIMIT 1);
    SET v_id_permission_dog_view := (SELECT PERMISSION.id_permission FROM parts.DOG_Permission PERMISSION WHERE PERMISSION.code = 'DOG_VIEW' LIMIT 1);
    SET v_id_access_level_view := (SELECT ACCESS_LEVEL.id_access_level FROM parts.DOG_Access_Level ACCESS_LEVEL WHERE ACCESS_LEVEL.code = 'VIEW' LIMIT 1);
    
    
    CALL parts.p_core_validate_guid ( a_guid );

    SET a_id_user := IFNULL(a_id_user, 0);
    /*
    SET a_get_all_file_type := IFNULL(a_get_all_file_type, 0);
    SET a_get_inactive_file_type := IFNULL(a_get_inactive_file_type, 0);
    SET a_ids_file_type := TRIM(IFNULL(a_ids_file_type, ''));
    SET a_names_file_type := TRIM(IFNULL(a_names_file_type, ''));
    SET a_get_all_image := IFNULL(a_get_all_image, 0);
    SET a_get_inactive_image := IFNULL(a_get_inactive_image, 0);
    SET a_ids_image := TRIM(IFNULL(a_ids_image, ''));
    SET a_names_image := TRIM(IFNULL(a_names_image, ''));
    */
    SET a_get_all_button_icon := IFNULL(a_get_all_button_icon, 0);
    SET a_get_inactive_button_icon := IFNULL(a_get_inactive_button_icon, 0);
    SET a_ids_button_icon := TRIM(IFNULL(a_ids_button_icon, ''));
    SET a_names_button_icon := TRIM(IFNULL(a_names_button_icon, ''));
    SET a_notes_button_icon := TRIM(IFNULL(a_notes_button_icon, ''));
    SET a_require_all_id_search_filters_met := IFNULL(a_require_all_id_search_filters_met, 1);
    SET a_require_any_id_search_filters_met := IFNULL(a_require_any_id_search_filters_met, 1);
    SET a_require_all_non_id_search_filters_met := IFNULL(a_require_all_non_id_search_filters_met, 0);
    SET a_require_any_non_id_search_filters_met := IFNULL(a_require_any_non_id_search_filters_met, 1);
    SET a_show_errors := IFNULL(a_show_errors, 0);
    SET a_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT 
              a_guid
			, a_id_user
            , a_get_all_file_type
            , a_get_inactive_file_type
            , a_ids_file_type
            , a_names_file_type
            , a_get_all_image
            , a_get_inactive_image
            , a_ids_image
            , a_names_image
            , a_get_all_button_icon
            , a_get_inactive_button_icon
            , a_ids_button_icon
            , a_names_button_icon
            , a_notes_button_icon
            , a_require_all_id_search_filters_met
            , a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met
            , a_require_any_non_id_search_filters_met
            , a_show_errors
            , a_debug
		;

        SELECT
              v_id_type_error_bad_data
            , v_id_type_error_no_permission
            , v_id_permission_dog_view
            , v_time_start
        ;
    END IF;

    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Notes_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Button_Icon_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Image_Calc_Button_Icon;
    
    CREATE TEMPORARY TABLE tmp_Image_Calc_Button_Icon (
		  id_image INT NOT NULL
        , id_file_type INT
        , id_dog INT
        , path VARCHAR(1024)
        , name VARCHAR(1024)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );
    
    CREATE TEMPORARY TABLE tmp_Button_Icon_Calc_Button_Icon (
		  id_button_icon INT NOT NULL
		, id_image INT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
        , does_image_already_exist_in_temp_table BIT NOT NULL
    );
    
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error_Calc_Button_Icon (
          id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
		, id_type INT NULL
		, code VARCHAR(250) NOT NULL
        , msg TEXT NOT NULL
	);
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Id_Calc_Button_Icon (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT NULL
	);
    DELETE FROM tmp_Split_Id_Calc_Button_Icon;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Name_Calc_Button_Icon (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT NULL
	);
    DELETE FROM tmp_Split_Name_Calc_Button_Icon;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Notes_Calc_Button_Icon (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT NULL
	);
    DELETE FROM tmp_Split_Notes_Calc_Button_Icon;
    
    SET v_has_filter_button_icon_id = CASE WHEN a_ids_button_icon <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_button_icon_name = CASE WHEN a_names_button_icon <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_button_icon_notes = CASE WHEN a_notes_button_icon <> '' THEN 1 ELSE 0 END;

    -- Call Calc Images
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Button_Icon t_ERROR INNER JOIN parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_file_type -- a_get_all_file_type
                , a_get_inactive_file_type -- a_get_inactive_file_type
                , a_ids_file_type -- a_ids_file_type
                , a_names_file_type -- a_names_file_type
                , 1 -- a_get_all_dog
                , 0 -- a_get_inactive_dog
                , '' -- a_ids_dog
                , '' -- a_names_dog
                , a_get_all_image -- a_get_all_image
                , a_get_inactive_image -- a_get_inactive_image
                , a_ids_image -- a_ids_image
                , a_names_image -- a_names_image
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL parts.p_dog_calc_image (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_file_type -- a_get_all_file_type
            , a_get_inactive_file_type -- a_get_inactive_file_type
            , a_ids_file_type -- a_ids_file_type
            , a_names_file_type -- a_names_file_type
            , 1 -- a_get_all_dog
            , 0 -- a_get_inactive_dog
            , '' -- a_ids_dog
            , '' -- a_names_dog
            , a_get_all_image -- a_get_all_image
            , a_get_inactive_image -- a_get_inactive_image
            , a_ids_image -- a_ids_image
            , a_names_image -- a_names_image
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Image_Calc_Button_Icon (
              id_image
            , id_file_type
            , id_dog
            , path
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              IMAGES_T.id_image
            , IMAGES_T.id_file_type
            , IMAGES_T.id_dog
            , IMAGES_T.path
            , IMAGES_T.name
            , IMAGES_T.active

            , IMAGES_T.does_meet_id_filters
            , IMAGES_T.does_meet_non_id_filters
        FROM parts.DOG_Image_Temp IMAGES_T
        WHERE IMAGES_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Image_Calc_Button_Icon;
        END IF;
    END IF;

    -- Button_Icons
    IF v_has_filter_button_icon_id = 1 THEN
		CALL parts.p_core_split(a_guid, a_ids_button_icon, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Id_Calc_Button_Icon (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CAST(SPLIT_T.substring AS DECIMAL(10,0)) AS as_int
		FROM parts.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = a_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL parts.p_core_clear_split( a_guid );
	END IF;

    IF v_has_filter_button_icon_name = 1 THEN
		CALL parts.p_core_split(a_guid, a_names_button_icon, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Name_Calc_Button_Icon (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CAST(SPLIT_T.substring AS DECIMAL(10,0)) AS as_int
		FROM parts.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = a_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL parts.p_core_clear_split( a_guid );
	END IF;

    IF v_has_filter_button_icon_notes = 1 THEN
		CALL parts.p_core_split(a_guid, a_notes_button_icon, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Notes_Calc_Button_Icon (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CAST(SPLIT_T.substring AS DECIMAL(10,0)) AS as_int
		FROM parts.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = a_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL parts.p_core_clear_split( a_guid );
	END IF;
    
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Button_Icon t_ERROR INNER JOIN parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
		IF EXISTS (
			SELECT * 
            FROM tmp_Split_Id_Calc_Button_Icon t_SPLIT_ID
            LEFT JOIN parts.DOG_Button_Icon BUTTON_ICON ON t_SPLIT_ID.as_int = BUTTON_ICON.id_button_icon 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(BUTTON_ICON.id_button_icon)
				OR  (
					BUTTON_ICON.active = 0
					AND a_get_inactive_button_icon = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error_Calc_Button_Icon (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Button_Icon IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id_Calc_Button_Icon t_SPLIT_ID
			LEFT JOIN parts.DOG_Button_Icon BUTTON_ICON ON t_SPLIT_ID.as_int = BUTTON_ICON.id_button_icon
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(BUTTON_ICON.id_button_icon)
				OR  (
					BUTTON_ICON.active = 0
					AND a_get_inactive_button_icon = 0
				)
			;
        /* Don't error on names, or notes not found
		ELSEIF EXISTS ()
        */
		ELSE
            IF a_debug = 1 THEN
                SELECT 'Button_Icon Filters';
                WITH 
                    Button_Icon_Id_Filter AS (
                        SELECT BUTTON_ICON.id_button_icon
                        FROM tmp_Split_Id_Calc_Button_Icon t_SPLIT_ID
                        INNER JOIN parts.DOG_Button_Icon BUTTON_ICON ON t_SPLIT_ID.as_int = BUTTON_ICON.id_button_icon
                    )
                    , Button_Icon_Name_Filter AS (
                        SELECT BUTTON_ICON.id_button_icon
                        FROM tmp_Split_Name_Calc_Button_Icon t_SPLIT_NAME
                        INNER JOIN parts.DOG_Button_Icon BUTTON_ICON ON BUTTON_ICON.name LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                        WHERE 
                                t_SPLIT_NAME.substring IS NOT NULL
                            AND t_SPLIT_NAME.substring <> ''
                    )
                    , Button_Icon_Notes_Filter AS (
                        SELECT BUTTON_ICON.id_button_icon
                        FROM tmp_Split_Notes_Calc_Button_Icon t_SPLIT_NOTES
                        INNER JOIN parts.DOG_Button_Icon BUTTON_ICON ON BUTTON_ICON.notes LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                        WHERE 
                                t_SPLIT_NOTES.substring IS NOT NULL
                            AND t_SPLIT_NOTES.substring <> ''
                    )
                    , Button_Icon_Filters AS (
                        SELECT 
                            BUTTON_ICON_COMBINED.id_button_icon
                            , MAX(BUTTON_ICON_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                            , MAX(BUTTON_ICON_COMBINED.does_meet_name_filter) AS does_meet_name_filter
                            , MAX(BUTTON_ICON_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                        FROM (
                            SELECT 
                                BUTTON_ICON_ID_FILTER.id_button_icon 
                                , 1 AS does_meet_id_filter
                                , 0 AS does_meet_name_filter
                                , 0 AS does_meet_notes_filter
                            FROM Button_Icon_Id_Filter BUTTON_ICON_ID_FILTER
                            UNION
                            SELECT 
                                BUTTON_ICON_NAME_FILTER.id_button_icon 
                                , 0 AS does_meet_id_filter
                                , 1 AS does_meet_name_filter
                                , 0 AS does_meet_notes_filter
                            FROM Button_Icon_Name_Filter BUTTON_ICON_NAME_FILTER
                            UNION
                            SELECT 
                                BUTTON_ICON_NOTES_FILTER.id_button_icon 
                                , 0 AS does_meet_id_filter
                                , 0 AS does_meet_name_filter
                                , 1 AS does_meet_notes_filter
                            FROM Button_Icon_Notes_Filter BUTTON_ICON_NOTES_FILTER
                        ) BUTTON_ICON_COMBINED
                        GROUP BY BUTTON_ICON_COMBINED.id_button_icon
                    )
                SELECT 
                      BUTTON_ICON.id_button_icon
                    , BUTTON_ICON.id_image
                    , BUTTON_ICON.code
                    , BUTTON_ICON.name
                    , BUTTON_ICON.notes
                    , CASE WHEN 
                            v_has_filter_button_icon_id = 0 
                        OR  BUTTON_ICON_FILTERS.does_meet_id_filter = 1
                    THEN 1 ELSE 0 END AS does_meet_non_id_filters
                    , CASE WHEN 
                            (
                                v_has_filter_button_icon_name = 0
                            AND v_has_filter_button_icon_notes = 0
                        )
                        OR  BUTTON_ICON_FILTERS.does_meet_name_filter = 1
                        OR  BUTTON_ICON_FILTERS.does_meet_notes_filter = 1
                    THEN 1 ELSE 0 END AS does_meet_id_filters
                    , CASE WHEN t_IMAGE.id_image IS NOT NULL THEN 1 ELSE 0 END AS does_image_already_exist_in_temp_table
                FROM parts.DOG_Button_Icon BUTTON_ICON
                LEFT JOIN tmp_Image_Calc_Button_Icon t_IMAGE ON BUTTON_ICON.id_image = t_IMAGE.id_image
                LEFT JOIN Button_Icon_Filters BUTTON_ICON_FILTERS ON BUTTON_ICON.id_button_icon = BUTTON_ICON_FILTERS.id_button_icon
                WHERE 
                    (
                            a_get_all_button_icon = 1
                        OR  (
                                v_has_filter_button_icon_id = 1
                            AND BUTTON_ICON_FILTERS.does_meet_id_filter = 1
                        )
                        OR  (
                                v_has_filter_button_icon_name = 1
                            AND BUTTON_ICON_FILTERS.does_meet_name_filter = 1
                        )
                        OR  (
                                v_has_filter_button_icon_notes = 1
                            AND BUTTON_ICON_FILTERS.does_meet_notes_filter = 1
                        )
                    )
                    AND (
                            a_get_inactive_button_icon = 1
                        OR  BUTTON_ICON.active = 1
                    )
                ;
            END IF;
			INSERT INTO tmp_Button_Icon_Calc_Button_Icon (
				  id_button_icon
                , id_image
                , does_meet_id_filters
                , does_meet_non_id_filters
                , does_image_already_exist_in_temp_table
			)
            WITH 
                  Button_Icon_Id_Filter AS (
                    SELECT BUTTON_ICON.id_button_icon
                    FROM tmp_Split_Id_Calc_Button_Icon t_SPLIT_ID
                    INNER JOIN parts.DOG_Button_Icon BUTTON_ICON ON t_SPLIT_ID.as_int = BUTTON_ICON.id_button_icon
                )
                , Button_Icon_Name_Filter AS (
                    SELECT BUTTON_ICON.id_button_icon
                    FROM tmp_Split_Name_Calc_Button_Icon t_SPLIT_NAME
                    INNER JOIN parts.DOG_Button_Icon BUTTON_ICON ON BUTTON_ICON.name LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                    WHERE 
                            t_SPLIT_NAME.substring IS NOT NULL
                        AND t_SPLIT_NAME.substring <> ''
                )
                , Button_Icon_Notes_Filter AS (
                    SELECT BUTTON_ICON.id_button_icon
                    FROM tmp_Split_Notes_Calc_Button_Icon t_SPLIT_NOTES
                    INNER JOIN demo.DOG_Button_Icon BUTTON_ICON ON BUTTON_ICON.notes LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                    WHERE 
                            t_SPLIT_NOTES.substring IS NOT NULL
                        AND t_SPLIT_NOTES.substring <> ''
                )
                , Button_Icon_Filters AS (
                    SELECT 
                          BUTTON_ICON_COMBINED.id_button_icon
                        , MAX(BUTTON_ICON_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                        , MAX(BUTTON_ICON_COMBINED.does_meet_name_filter) AS does_meet_name_filter
                        , MAX(BUTTON_ICON_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                    FROM (
                        SELECT 
                              BUTTON_ICON_ID_FILTER.id_button_icon 
                            , 1 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                            , 0 AS does_meet_notes_filter
                        FROM Button_Icon_Id_Filter BUTTON_ICON_ID_FILTER
                        UNION
                        SELECT 
                              BUTTON_ICON_NAME_FILTER.id_button_icon 
                            , 0 AS does_meet_id_filter
                            , 1 AS does_meet_name_filter
                            , 0 AS does_meet_notes_filter
                        FROM Button_Icon_Name_Filter BUTTON_ICON_NAME_FILTER
                        UNION
                        SELECT 
                              BUTTON_ICON_NOTES_FILTER.id_button_icon 
                            , 0 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                            , 1 AS does_meet_notes_filter
                        FROM Button_Icon_Notes_Filter BUTTON_ICON_NOTES_FILTER
                    ) BUTTON_ICON_COMBINED
                    GROUP BY BUTTON_ICON_COMBINED.id_button_icon
                )
            SELECT 
                  BUTTON_ICON.id_button_icon
                , BUTTON_ICON.id_image
                , CASE WHEN 
                        v_has_filter_button_icon_id = 0 
                    OR  IFNULL(BUTTON_ICON_FILTERS.does_meet_id_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_id_filters
                , CASE WHEN 
                        (
                            v_has_filter_button_icon_name = 0
                        AND v_has_filter_button_icon_notes = 0
                    )
                    OR  IFNULL(BUTTON_ICON_FILTERS.does_meet_name_filter, 0) = 1
                    OR  IFNULL(BUTTON_ICON_FILTERS.does_meet_notes_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_non_id_filters
                , CASE WHEN t_IMAGE.id_image IS NOT NULL THEN 1 ELSE 0 END AS does_image_already_exist_in_temp_table
            FROM parts.DOG_Button_Icon BUTTON_ICON
            LEFT JOIN tmp_Image_Calc_Button_Icon t_IMAGE ON BUTTON_ICON.id_image = t_IMAGE.id_image
            LEFT JOIN Button_Icon_Filters BUTTON_ICON_FILTERS ON BUTTON_ICON.id_button_icon = BUTTON_ICON_FILTERS.id_button_icon
            WHERE 
                (
                        a_get_all_button_icon = 1
                    OR  (
                            v_has_filter_button_icon_id = 1
                        AND BUTTON_ICON_FILTERS.does_meet_id_filter = 1
                    )
                    OR  (
                            v_has_filter_button_icon_name = 1
                        AND BUTTON_ICON_FILTERS.does_meet_name_filter = 1
                    )
                    OR  (
                            v_has_filter_button_icon_notes = 1
                        AND BUTTON_ICON_FILTERS.does_meet_notes_filter = 1
                    )
                )
                AND (
                        a_get_inactive_button_icon = 1
                    OR  BUTTON_ICON.active = 1
                )
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split_Id_Calc_Button_Icon;
    DELETE FROM tmp_Split_Name_Calc_Button_Icon;
    DELETE FROM tmp_Split_Notes_Calc_Button_Icon;

    IF a_debug = 1 THEN
        SELECT 'After get Button Icons and Images';
        SELECT * FROM tmp_Image_Calc_Button_Icon;
        SELECT * FROM tmp_Button_Icon_Calc_Button_Icon;
    END IF;
    
    -- Missing Images
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Button_Icon t_ERROR INNER JOIN parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
       
        IF 
                a_require_all_id_search_filters_met = 0 
            AND a_require_all_non_id_search_filters_met = 0
        THEN
            INSERT INTO tmp_Image_Calc_Button_Icon (
                  id_image
                , does_meet_id_filters
                , does_meet_non_id_filters
            )
            SELECT DISTINCT
                  IMAGE.id_image
                , 0 AS does_meet_id_filters
                , 0 AS does_meet_non_id_filters
            FROM parts.DOG_Image IMAGE
            INNER JOIN tmp_Button_Icon_Calc_Button_Icon t_BUTTON_ICON 
                ON  IMAGE.id_image = t_BUTTON_ICON.id_image
                AND t_BUTTON_ICON.does_image_already_exist_in_temp_table = 0
            ;
        END IF;
    END IF;

    -- Filter records
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Button_Icon t_ERROR INNER JOIN parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        DELETE t_BUTTON_ICON
        FROM tmp_Button_Icon_Calc_Button_Icon t_BUTTON_ICON
        LEFT JOIN tmp_Image_Calc_Button_Icon t_IMAGE ON t_BUTTON_ICON.id_image = t_IMAGE.id_image
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        t_BUTTON_ICON.does_meet_id_filters = 0
                    OR  IFNULL(t_IMAGE.does_meet_id_filters, 1) = 0
                )
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND (
                        t_BUTTON_ICON.does_meet_non_id_filters = 0
                    OR  IFNULL(t_IMAGE.does_meet_non_id_filters, 1) = 0
                )
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND t_BUTTON_ICON.does_meet_id_filters = 0
                AND IFNULL(t_IMAGE.does_meet_id_filters, 1) = 0
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND t_BUTTON_ICON.does_meet_non_id_filters = 0
                AND IFNULL(t_IMAGE.does_meet_non_id_filters, 1) = 0
            )
        ;

        IF a_debug = 1 THEN
            WITH 
                Image_And_Best_Button_Icon AS (
                    SELECT 
                        t_BUTTON_ICON.id_image
                        , MAX(IFNULL(t_BUTTON_ICON.does_meet_id_filters, 0)) AS does_meet_id_filters
                        , MAX(IFNULL(t_BUTTON_ICON.does_meet_non_id_filters, 0)) AS does_meet_non_id_filters
                    FROM tmp_Button_Icon_Calc_Button_Icon t_BUTTON_ICON 
                    GROUP BY t_BUTTON_ICON.id_image
                )
            SELECT *
            FROM tmp_Image_Calc_Button_Icon t_IMAGE
            LEFT JOIN Image_And_Best_Button_Icon IMAGE_BEST ON t_IMAGE.id_image = IMAGE_BEST.id_image
            WHERE
                    (
                        a_require_all_id_search_filters_met = 1
                    AND (
                            IMAGE_BEST.does_meet_id_filters = 0
                        OR  IFNULL(t_IMAGE.does_meet_id_filters, 1) = 0
                    )
                )
                OR  (
                        a_require_all_non_id_search_filters_met = 1
                    AND (
                            IMAGE_BEST.does_meet_non_id_filters = 0
                        OR  IFNULL(t_IMAGE.does_meet_non_id_filters, 1) = 0
                    )
                )
                OR  (
                    a_require_any_id_search_filters_met = 1
                    AND IMAGE_BEST.does_meet_id_filters = 0
                    AND IFNULL(t_IMAGE.does_meet_id_filters, 1) = 0
                )
                OR  (
                    a_require_any_non_id_search_filters_met = 1
                    AND IMAGE_BEST.does_meet_non_id_filters = 0
                    AND IFNULL(t_IMAGE.does_meet_non_id_filters, 1) = 0
                )
            ;
        END IF;

        WITH 
            Image_And_Best_Button_Icon AS (
                SELECT 
                      t_BUTTON_ICON.id_image
                    , MAX(IFNULL(t_BUTTON_ICON.does_meet_id_filters, 0)) AS does_meet_id_filters
                    , MAX(IFNULL(t_BUTTON_ICON.does_meet_non_id_filters, 0)) AS does_meet_non_id_filters
                FROM tmp_Button_Icon_Calc_Button_Icon t_BUTTON_ICON
                GROUP BY t_BUTTON_ICON.id_image
            )
        DELETE t_IMAGE
        FROM tmp_Image_Calc_Button_Icon t_IMAGE
        LEFT JOIN Image_And_Best_Button_Icon IMAGE_BEST ON t_IMAGE.id_image = IMAGE_BEST.id_image
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        IFNULL(IMAGE_BEST.does_meet_id_filters, 0) = 0
                    OR  IFNULL(t_IMAGE.does_meet_id_filters, 1) = 0
                )
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND (
                        IFNULL(IMAGE_BEST.does_meet_non_id_filters, 0) = 0
                    OR  IFNULL(t_IMAGE.does_meet_non_id_filters, 1) = 0
                )
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND IFNULL(IMAGE_BEST.does_meet_id_filters, 0) = 0
                AND IFNULL(t_IMAGE.does_meet_id_filters, 1) = 0
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND IFNULL(IMAGE_BEST.does_meet_non_id_filters, 0) = 0
                AND IFNULL(t_IMAGE.does_meet_non_id_filters, 1) = 0
            )
        ;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'After filter Button_Icons';
        SELECT * FROM tmp_Image_Calc_Button_Icon;
        SELECT * FROM tmp_Button_Icon_Calc_Button_Icon;
    END IF;


    -- Permissions
    IF a_debug = 1 THEN
		SELECT 
			  a_guid -- a_guid
            , 0 -- get_all_user
            , 0 -- get_inactive_user
            , a_id_user -- ids_user
            , '' -- a_auth0_ids_user
            , '' -- a_names_user
            , '' -- a_emails_user
            , 1 -- a_require_all_id_search_filters_met
            , 1 -- a_require_any_id_search_filters_met
            , 0 -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met
            , v_id_permission_dog_view -- ids_permission
            , v_id_access_level_view -- ids_access_level
            , 0 -- a_show_errors
			, 0 -- a_debug
        ;
    END IF;
    
	CALL parts.p_dog_calc_user(
          a_guid -- a_guid
        , 0 -- get_all_user
        , 0 -- get_inactive_user
        , a_id_user -- ids_user
        , '' -- a_auth0_ids_user
        , '' -- a_names_user
        , '' -- a_emails_user
        , 1 -- a_require_all_id_search_filters_met
        , 1 -- a_require_any_id_search_filters_met
        , 0 -- a_require_all_non_id_search_filters_met
        , 0 -- a_require_any_non_id_search_filters_met
        , v_id_permission_dog_view -- ids_permission
        , v_id_access_level_view -- ids_access_level
        , 0 -- a_show_errors
        , 0 -- a_debug
    );
	
    SELECT
          IFNULL(CALC_USER_T.has_access, 0)
    INTO
          v_can_view
    FROM parts.DOG_Calc_User_Temp CALC_USER_T
    WHERE CALC_USER_T.GUID = a_guid
    LIMIT 1
    ;
    
    IF a_debug = 1 THEN
        SELECT v_can_view;
    END IF;
    
    IF (v_can_view = 0) THEN
		DELETE t_ME
        FROM tmp_Msg_Error_Calc_Button_Icon t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error_Calc_Button_Icon (
			  id_type
			, code
            , msg
		)
		VALUES (
			  v_id_type_error_no_permission
            , v_code_type_error_no_permission
			, 'You do not have permission to view Dogs and Button Icons.'
		)
        ;
    END IF;
	
	CALL parts.p_dog_clear_calc_user(
		a_guid
        , 0 -- a_debug
	);

    IF a_debug = 1 THEN
        SELECT 'Before non-permitted data deletion';
        SELECT * FROM tmp_Image_Calc_Button_Icon;
        SELECT * FROM tmp_Button_Icon_Calc_Button_Icon;
        SELECT * FROM tmp_Msg_Error_Calc_Button_Icon;
    END IF;
    
    IF EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Button_Icon t_ERROR INNER JOIN parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Image_Calc_Button_Icon;
            SELECT * FROM tmp_Button_Icon_Calc_Button_Icon;
        END IF;

        DELETE FROM tmp_Button_Icon_Calc_Button_Icon;
        DELETE FROM tmp_Image_Calc_Button_Icon;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'After non-permitted data deletion';
    END IF;

    -- Outputs
	START TRANSACTION;
        -- Button_Icons
        INSERT INTO parts.DOG_Button_Icon_Temp (
            guid
            , id_button_icon
            , id_image
            , code
            , name
            , notes
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT 
            a_guid
            , t_BUTTON_ICON.id_button_icon
            , t_BUTTON_ICON.id_image
            , BUTTON_ICON.code
            , BUTTON_ICON.name
            , BUTTON_ICON.notes
            , BUTTON_ICON.active

            , t_BUTTON_ICON.does_meet_id_filters
            , t_BUTTON_ICON.does_meet_non_id_filters
        FROM parts.DOG_Button_Icon BUTTON_ICON
        INNER JOIN tmp_Button_Icon_Calc_Button_Icon t_BUTTON_ICON ON BUTTON_ICON.id_button_icon = t_BUTTON_ICON.id_button_icon
        ;
    COMMIT;

    -- Errors
    IF a_show_errors = 1 THEN
        SELECT 
            t_ERROR.id_error
            , t_ERROR.id_type
            , t_ERROR.code
            , ERROR_TYPE.name
            , ERROR_TYPE.description
            , ERROR_TYPE.is_breaking_error
            , ERROR_TYPE.background_colour
            , ERROR_TYPE.text_colour
            , t_ERROR.msg
        FROM tmp_Msg_Error_Calc_Button_Icon t_ERROR
        INNER JOIN parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;
    END IF;

    IF a_debug = 1 AND v_can_view = 1 THEN
        SELECT * FROM tmp_Image_Calc_Button_Icon;
        SELECT * FROM tmp_Button_Icon_Calc_Button_Icon;
    END IF;
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Notes_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Button_Icon_Calc_Button_Icon;
    DROP TEMPORARY TABLE IF EXISTS tmp_Image_Calc_Button_Icon;
    
    IF a_debug = 1 THEN
		CALL parts.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*


CALL parts.p_dog_calc_button_icon (
      'slips                               ' -- a_guid
	, 1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_file_type
	, 0                                     	-- a_get_inactive_file_type
	, ''                                    	-- a_ids_file_type
    , ''                                        -- a_names_file_type
    , 1                                     	-- a_get_all_image
	, 0                                     	-- a_get_inactive_image
	, ''                                    	-- a_ids_image
    , ''                                        -- a_names_image
    , 1                                     	-- a_get_all_button_icon
	, 0                                     	-- a_get_inactive_button_icon
	, ''                                    	-- a_ids_button_icon
    , ''                                        -- a_names_button_icon
    , ''                                        -- a_notes_button_icon
    , 0                                         -- a_require_all_id_search_filters_met
    , 0                                         -- a_require_any_id_search_filters_met
    , 0                                         -- a_require_all_non_id_search_filters_met
    , 0                                         -- a_require_any_non_id_search_filters_met
    , 0                                         -- a_show_errors
    , 0                                     	-- a_debug
);

CALL parts.p_dog_calc_button_icon (
      'slips                               ' -- a_guid
	, 1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_file_type
	, 0                                     	-- a_get_inactive_file_type
	, ''                                    	-- a_ids_file_type
    , 'pat,point'                                        -- a_names_file_type
    , 1                                     	-- a_get_all_image
	, 0                                     	-- a_get_inactive_image
	, ''                                    	-- a_ids_image
    , 'pat,point'                                        -- a_names_image
    , 1                                     	-- a_get_all_button_icon
	, 0                                     	-- a_get_inactive_button_icon
	, ''                                    	-- a_ids_button_icon
    , 'pat,point'                                        -- a_names_button_icon
    , 'pat,point'                                        -- a_notes_button_icon
    , 1                                         -- a_require_all_id_search_filters_met
    , 1                                         -- a_require_any_id_search_filters_met
    , 0                                         -- a_require_all_non_id_search_filters_met
    , 1                                         -- a_require_any_non_id_search_filters_met
    , 1                                         -- a_show_errors
    , 0                                     	-- a_debug
);
SELECT *
FROM parts.DOG_Button_Icon_Temp
;

CALL parts.p_dog_clear_calc_button_icon (
	'slips                               ' -- a_guid
    , 1 -- debug
);

DELETE
FROM parts.DOG_Button_Icon_Temp
;

*/
