<?php
/**
 * Theme storage manipulations
 *
 * @package TOURIMO
 * @since TOURIMO 1.0
 */

// Disable direct call
if ( ! defined( 'ABSPATH' ) ) {
	exit; }

// Get theme variable
if ( ! function_exists( 'tourimo_storage_get' ) ) {
	function tourimo_storage_get( $var_name, $default = '' ) {
		global $TOURIMO_STORAGE;
		return isset( $TOURIMO_STORAGE[ $var_name ] ) ? $TOURIMO_STORAGE[ $var_name ] : $default;
	}
}

// Set theme variable
if ( ! function_exists( 'tourimo_storage_set' ) ) {
	function tourimo_storage_set( $var_name, $value ) {
		global $TOURIMO_STORAGE;
		$TOURIMO_STORAGE[ $var_name ] = $value;
	}
}

// Check if theme variable is empty
if ( ! function_exists( 'tourimo_storage_empty' ) ) {
	function tourimo_storage_empty( $var_name, $key = '', $key2 = '' ) {
		global $TOURIMO_STORAGE;
		if ( ! empty( $key ) && ! empty( $key2 ) ) {
			return empty( $TOURIMO_STORAGE[ $var_name ][ $key ][ $key2 ] );
		} elseif ( ! empty( $key ) ) {
			return empty( $TOURIMO_STORAGE[ $var_name ][ $key ] );
		} else {
			return empty( $TOURIMO_STORAGE[ $var_name ] );
		}
	}
}

// Check if theme variable is set
if ( ! function_exists( 'tourimo_storage_isset' ) ) {
	function tourimo_storage_isset( $var_name, $key = '', $key2 = '' ) {
		global $TOURIMO_STORAGE;
		if ( ! empty( $key ) && ! empty( $key2 ) ) {
			return isset( $TOURIMO_STORAGE[ $var_name ][ $key ][ $key2 ] );
		} elseif ( ! empty( $key ) ) {
			return isset( $TOURIMO_STORAGE[ $var_name ][ $key ] );
		} else {
			return isset( $TOURIMO_STORAGE[ $var_name ] );
		}
	}
}

// Delete theme variable
if ( ! function_exists( 'tourimo_storage_unset' ) ) {
	function tourimo_storage_unset( $var_name, $key = '', $key2 = '' ) {
		global $TOURIMO_STORAGE;
		if ( ! empty( $key ) && ! empty( $key2 ) ) {
			unset( $TOURIMO_STORAGE[ $var_name ][ $key ][ $key2 ] );
		} elseif ( ! empty( $key ) ) {
			unset( $TOURIMO_STORAGE[ $var_name ][ $key ] );
		} else {
			unset( $TOURIMO_STORAGE[ $var_name ] );
		}
	}
}

// Inc/Dec theme variable with specified value
if ( ! function_exists( 'tourimo_storage_inc' ) ) {
	function tourimo_storage_inc( $var_name, $value = 1 ) {
		global $TOURIMO_STORAGE;
		if ( empty( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = 0;
		}
		$TOURIMO_STORAGE[ $var_name ] += $value;
	}
}

// Concatenate theme variable with specified value
if ( ! function_exists( 'tourimo_storage_concat' ) ) {
	function tourimo_storage_concat( $var_name, $value ) {
		global $TOURIMO_STORAGE;
		if ( empty( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = '';
		}
		$TOURIMO_STORAGE[ $var_name ] .= $value;
	}
}

// Get array (one or two dim) element
if ( ! function_exists( 'tourimo_storage_get_array' ) ) {
	function tourimo_storage_get_array( $var_name, $key, $key2 = '', $default = '' ) {
		global $TOURIMO_STORAGE;
		if ( empty( $key2 ) ) {
			return ! empty( $var_name ) && ! empty( $key ) && isset( $TOURIMO_STORAGE[ $var_name ][ $key ] ) ? $TOURIMO_STORAGE[ $var_name ][ $key ] : $default;
		} else {
			return ! empty( $var_name ) && ! empty( $key ) && isset( $TOURIMO_STORAGE[ $var_name ][ $key ][ $key2 ] ) ? $TOURIMO_STORAGE[ $var_name ][ $key ][ $key2 ] : $default;
		}
	}
}

// Set array element
if ( ! function_exists( 'tourimo_storage_set_array' ) ) {
	function tourimo_storage_set_array( $var_name, $key, $value ) {
		global $TOURIMO_STORAGE;
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = array();
		}
		if ( '' === $key ) {
			$TOURIMO_STORAGE[ $var_name ][] = $value;
		} else {
			$TOURIMO_STORAGE[ $var_name ][ $key ] = $value;
		}
	}
}

// Set two-dim array element
if ( ! function_exists( 'tourimo_storage_set_array2' ) ) {
	function tourimo_storage_set_array2( $var_name, $key, $key2, $value ) {
		global $TOURIMO_STORAGE;
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = array();
		}
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ][ $key ] ) ) {
			$TOURIMO_STORAGE[ $var_name ][ $key ] = array();
		}
		if ( '' === $key2 ) {
			$TOURIMO_STORAGE[ $var_name ][ $key ][] = $value;
		} else {
			$TOURIMO_STORAGE[ $var_name ][ $key ][ $key2 ] = $value;
		}
	}
}

// Merge array elements
if ( ! function_exists( 'tourimo_storage_merge_array' ) ) {
	function tourimo_storage_merge_array( $var_name, $key, $value ) {
		global $TOURIMO_STORAGE;
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = array();
		}
		if ( '' === $key ) {
			$TOURIMO_STORAGE[ $var_name ] = array_merge( $TOURIMO_STORAGE[ $var_name ], $value );
		} else {
			$TOURIMO_STORAGE[ $var_name ][ $key ] = array_merge( $TOURIMO_STORAGE[ $var_name ][ $key ], $value );
		}
	}
}

// Add array element after the key
if ( ! function_exists( 'tourimo_storage_set_array_after' ) ) {
	function tourimo_storage_set_array_after( $var_name, $after, $key, $value = '' ) {
		global $TOURIMO_STORAGE;
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = array();
		}
		if ( is_array( $key ) ) {
			tourimo_array_insert_after( $TOURIMO_STORAGE[ $var_name ], $after, $key );
		} else {
			tourimo_array_insert_after( $TOURIMO_STORAGE[ $var_name ], $after, array( $key => $value ) );
		}
	}
}

// Add array element before the key
if ( ! function_exists( 'tourimo_storage_set_array_before' ) ) {
	function tourimo_storage_set_array_before( $var_name, $before, $key, $value = '' ) {
		global $TOURIMO_STORAGE;
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = array();
		}
		if ( is_array( $key ) ) {
			tourimo_array_insert_before( $TOURIMO_STORAGE[ $var_name ], $before, $key );
		} else {
			tourimo_array_insert_before( $TOURIMO_STORAGE[ $var_name ], $before, array( $key => $value ) );
		}
	}
}

// Push element into array
if ( ! function_exists( 'tourimo_storage_push_array' ) ) {
	function tourimo_storage_push_array( $var_name, $key, $value ) {
		global $TOURIMO_STORAGE;
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = array();
		}
		if ( '' === $key ) {
			array_push( $TOURIMO_STORAGE[ $var_name ], $value );
		} else {
			if ( ! isset( $TOURIMO_STORAGE[ $var_name ][ $key ] ) ) {
				$TOURIMO_STORAGE[ $var_name ][ $key ] = array();
			}
			array_push( $TOURIMO_STORAGE[ $var_name ][ $key ], $value );
		}
	}
}

// Pop element from array
if ( ! function_exists( 'tourimo_storage_pop_array' ) ) {
	function tourimo_storage_pop_array( $var_name, $key = '', $defa = '' ) {
		global $TOURIMO_STORAGE;
		$rez = $defa;
		if ( '' === $key ) {
			if ( isset( $TOURIMO_STORAGE[ $var_name ] ) && is_array( $TOURIMO_STORAGE[ $var_name ] ) && count( $TOURIMO_STORAGE[ $var_name ] ) > 0 ) {
				$rez = array_pop( $TOURIMO_STORAGE[ $var_name ] );
			}
		} else {
			if ( isset( $TOURIMO_STORAGE[ $var_name ][ $key ] ) && is_array( $TOURIMO_STORAGE[ $var_name ][ $key ] ) && count( $TOURIMO_STORAGE[ $var_name ][ $key ] ) > 0 ) {
				$rez = array_pop( $TOURIMO_STORAGE[ $var_name ][ $key ] );
			}
		}
		return $rez;
	}
}

// Inc/Dec array element with specified value
if ( ! function_exists( 'tourimo_storage_inc_array' ) ) {
	function tourimo_storage_inc_array( $var_name, $key, $value = 1 ) {
		global $TOURIMO_STORAGE;
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = array();
		}
		if ( empty( $TOURIMO_STORAGE[ $var_name ][ $key ] ) ) {
			$TOURIMO_STORAGE[ $var_name ][ $key ] = 0;
		}
		$TOURIMO_STORAGE[ $var_name ][ $key ] += $value;
	}
}

// Concatenate array element with specified value
if ( ! function_exists( 'tourimo_storage_concat_array' ) ) {
	function tourimo_storage_concat_array( $var_name, $key, $value ) {
		global $TOURIMO_STORAGE;
		if ( ! isset( $TOURIMO_STORAGE[ $var_name ] ) ) {
			$TOURIMO_STORAGE[ $var_name ] = array();
		}
		if ( empty( $TOURIMO_STORAGE[ $var_name ][ $key ] ) ) {
			$TOURIMO_STORAGE[ $var_name ][ $key ] = '';
		}
		$TOURIMO_STORAGE[ $var_name ][ $key ] .= $value;
	}
}

// Call object's method
if ( ! function_exists( 'tourimo_storage_call_obj_method' ) ) {
	function tourimo_storage_call_obj_method( $var_name, $method, $param = null ) {
		global $TOURIMO_STORAGE;
		if ( null === $param ) {
			return ! empty( $var_name ) && ! empty( $method ) && isset( $TOURIMO_STORAGE[ $var_name ] ) ? $TOURIMO_STORAGE[ $var_name ]->$method() : '';
		} else {
			return ! empty( $var_name ) && ! empty( $method ) && isset( $TOURIMO_STORAGE[ $var_name ] ) ? $TOURIMO_STORAGE[ $var_name ]->$method( $param ) : '';
		}
	}
}

// Get object's property
if ( ! function_exists( 'tourimo_storage_get_obj_property' ) ) {
	function tourimo_storage_get_obj_property( $var_name, $prop, $default = '' ) {
		global $TOURIMO_STORAGE;
		return ! empty( $var_name ) && ! empty( $prop ) && isset( $TOURIMO_STORAGE[ $var_name ]->$prop ) ? $TOURIMO_STORAGE[ $var_name ]->$prop : $default;
	}
}
