<?php
/***************************************** for input fields like text, numbers, email and website **************************************/

$select_icon = $dom->createElement('i');
$select_icon->setAttribute( 'class', 'mdc-select__dropdown-icon' );

if ( in_array( $field->type, $date_field ) ) {
	//$input_classes_types = array( 'ginput_container_date' );
	$contains_error = false;
	$contains_placeholder = false;
	$input_id = '';
	$all_divs = $dom->getElementsByTagName( 'div' );
	$is_date_boxes_field = false;
	$date_boxes_classes = array( 'gfield_date_day', 'gfield_date_month', 'gfield_date_year' );
	$is_date_dropdown_field = false;
	$date_dropdown_classes = array( 'gfield_date_dropdown_day', 'gfield_date_dropdown_month', 'gfield_date_dropdown_year' );

	//get the first div
	foreach ( $all_divs as $div ) {
		$div_classes = $div->getAttribute( 'class' );

		$div_classes_array = explode( ' ', $div_classes );


		$is_valid_input_field = false;

		foreach ( $div_classes_array as $div_class_value ) {
			// if ( in_array( $div_class_value, $input_classes_types ) ) {
			//  $is_valid_input_field = true;
			// }
			if ( in_array( $div_class_value, $date_boxes_classes ) ) {
				$is_date_boxes_field = true;
			}
			if ( in_array( $div_class_value, $date_dropdown_classes ) ) {
				$is_date_dropdown_field = true;
			}
		}
	}
	foreach ( $all_divs as $div ) {
		$div_classes = $div->getAttribute( 'class' );
		$div_classes_array = explode( ' ', $div_classes );
			$is_valid_datepicker_container = false;
			foreach ( $div_classes_array as $div_class_value ) {
				if ( $div_class_value === 'ginput_container_date' )  {
					$is_valid_datepicker_container = true;
				}
			}
		// for datepicker date field
		if (  ! $is_date_dropdown_field && ! $is_date_boxes_field && $is_valid_datepicker_container ) {

			//add class to input
			$inputs = $div->getElementsByTagName( 'input' );
			foreach ( $inputs as $input ) {
				$id = $input->getAttribute( 'id' );
				//$input_type = $input->getAttribute( 'type' );
				if ( preg_match( '/^input_/', $id ) ) {
					$input_id = $id;
					$input_classes = $input->getAttribute( 'class' );
					$input->setAttribute( 'class', $input_classes . ' mdc-text-field__input' );

					// check if contains placeholder
					if ( $input->getAttribute( 'placeholder' ) !== ''  && $type !== 'outlined' ) {
						$contains_placeholder = true;
					}
				}
			}

			// get icons
			$icons = $div->getElementsByTagName('i');
			$icon_parent_class = '';
			foreach( $icons as $icon ) {
				$icon_classes = $icon->getAttribute('class');
				if ( strpos( $icon_classes, 'stla-fontawesome-icon') !== false) {
					$icon_parent_class = 'mdc-text-field--with-leading-icon';
					$icon->setAttribute('class', $icon->getAttribute('class') . ' mdc-text-field__icon');
				}
			}

			//add classes to div
			$add_div_classes = ' mdc-text-field '.$icon_parent_class;

			// outlined design
			$add_div_classes .= $type === 'outlined' ? ' mdc-text-field--outlined': '';

			//if has error then add the error class
			if ( $contains_error ) {
				$add_div_classes .= ' mdc-text-field--invalid';
			}
			$div->setAttribute( 'class', $div->getAttribute( 'class' ) . $add_div_classes );
			$div->setAttribute( 'data-mdc-auto-init', 'MDCTextField' );

			//get the first label
			$labels = $dom->getElementsByTagName( 'label' );
			foreach ( $labels as $label ) {
				$label_for = $label->getAttribute( 'for' );

				if ( $input_id === $label_for ) {
					$add_label_classes = ' mdc-floating-label';
					if ( $contains_placeholder ) {
						$add_label_classes .= ' mdc-floating-label--float-above';
					}

					// outlined design
					if( $type === 'outlined' ) {
						$notched_outline = $dom->createElement('div');
						$notched_outline->setAttribute( 'class', 'mdc-notched-outline' );

						$notched_outline_leading = $dom->createElement( 'div' );
						$notched_outline_leading->setAttribute( 'class', 'mdc-notched-outline__leading' );
						
						$notched_outline_notch = $dom->createElement( 'div' );
						$notched_outline_notch->setAttribute( 'class', 'mdc-notched-outline__notch' );

						$notched_outline_trailing = $dom->createElement( 'div' );
						$notched_outline_trailing->setAttribute( 'class', 'mdc-notched-outline__trailing' );
						
						if ($contains_placeholder) {
							$notched_outline->setAttribute( 'class', 'mdc-notched-outline mdc-notched-outline--notched' );
						}
						$div->appendChild( $notched_outline );

						$notched_outline->appendChild( $notched_outline_leading );
						$notched_outline->appendChild( $notched_outline_notch );
						$notched_outline->appendChild( $notched_outline_trailing );

						$label->setAttribute('class', $label->getAttribute( 'class' ) . $add_label_classes );
						$notched_outline_notch->appendChild( $label );
					}

					else{
						$label->setAttribute( 'class', $label->getAttribute( 'class' ) . $add_label_classes );
						$div->appendChild( $label );
					}
				}
			}

			//create a new div
            if ( $type === 'filled' ) {
                //create a new div
                //using a div here is making the site to not load.. temporary solution.. use span
                $bottom_line_div = $dom->createElement('span');
                $bottom_line_div->setAttribute('class', 'mdc-line-ripple');
                // var_dump($bottom_line_div);
                $div->appendChild($bottom_line_div);
			}

		}

		//for date boxes date field

		if ( $is_date_boxes_field ) {
			$div_classes = $div->getAttribute( 'class' );
		$div_classes_array = explode( ' ', $div_classes );
			$is_valid_box_div = false;
			foreach ( $div_classes_array as $div_class_value ) {
				if ( in_array( $div_class_value, $date_boxes_classes ) ) {
					$is_valid_box_div = true;
				}
			}
			if ( $is_valid_box_div ) {
				//add class to input
				$inputs = $div->getElementsByTagName( 'input' );
				foreach ( $inputs as $input ) {
					$id = $input->getAttribute( 'id' );

					//$input_type = $input->getAttribute( 'type' );
					if ( preg_match( '/^input_/', $id ) ) {
						$input_id = $id;
						$input_classes = $input->getAttribute( 'class' );
						$input->setAttribute( 'class', $input_classes . ' mdc-text-field__input' );

						// check if contains placeholder
						if ( $input->getAttribute( 'placeholder' ) !== ''  && $type !== 'outlined' ) {
							$contains_placeholder = true;
						}
					}
				}



				//add classes to div
				$add_div_classes = ' mdc-text-field ';

				// outlined design
				$add_div_classes .= $type === 'outlined' ? ' mdc-text-field--outlined': '';

				//if has error then add the error class
				if ( $contains_error ) {
					$add_div_classes .= ' mdc-text-field--invalid';
				}
				$div->setAttribute( 'class', $div->getAttribute( 'class' ) . $add_div_classes );
				$div->setAttribute( 'data-mdc-auto-init', 'MDCTextField' );

				//get the first label
				$labels = $div->getElementsByTagName( 'label' );
				foreach ( $labels as $label ) {
					$label_for = $label->getAttribute( 'for' );
					$label_classes = $label->getAttribute( 'class' );
					$label_classes_array = explode( ' ', $label_classes );
					//var_dump( $input_id );

					if ( $input_id === $label_for ) {
						$add_label_classes = ' mdc-floating-label';
						if ( $contains_placeholder ) {
							$add_label_classes .= ' mdc-floating-label--float-above';
						}

						// outlined design
						if( $type === 'outlined' ) {
							$notched_outline = $dom->createElement('div');
							$notched_outline->setAttribute( 'class', 'mdc-notched-outline' );

							$notched_outline_leading = $dom->createElement( 'div' );
							$notched_outline_leading->setAttribute( 'class', 'mdc-notched-outline__leading' );
							
							$notched_outline_notch = $dom->createElement( 'div' );
							$notched_outline_notch->setAttribute( 'class', 'mdc-notched-outline__notch' );

							$notched_outline_trailing = $dom->createElement( 'div' );
							$notched_outline_trailing->setAttribute( 'class', 'mdc-notched-outline__trailing' );
							
							if ($contains_placeholder) {
								$notched_outline->setAttribute( 'class', 'mdc-notched-outline mdc-notched-outline--notched' );
							}
							$div->appendChild( $notched_outline );

							$notched_outline->appendChild( $notched_outline_leading );
							$notched_outline->appendChild( $notched_outline_notch );
							$notched_outline->appendChild( $notched_outline_trailing );

							$label->setAttribute('class', $label->getAttribute( 'class' ) . $add_label_classes );
							$notched_outline_notch->appendChild( $label );
						}
						else{
							$label->setAttribute( 'class', $label->getAttribute( 'class' ) . $add_label_classes );
							$div->appendChild( $label );
						}	
					}
				}

				//create a new div
                if ($type === 'filled') {
                    $bottom_line_div = $dom->createElement('div');
                    $bottom_line_div->setAttribute('class', 'mdc-line-ripple');
                    $div->appendChild($bottom_line_div);
                }
			}
		}


		//for date dropdown fields
		if ( $is_date_dropdown_field ) {
			$div_classes = $div->getAttribute( 'class' );
			$div_classes_array = explode( ' ', $div_classes );
			$is_valid_dropdown_div = false;
			foreach ( $div_classes_array as $div_class_value ) {
				if ( in_array( $div_class_value, $date_dropdown_classes ) ) {
					$is_valid_dropdown_div = true;
				}
			}
			
			if ( $is_valid_dropdown_div ) {

				//add class to input
				$get_selects = $div->getElementsByTagName( 'select' );
				//var_dump($get_input->getAttribute('placeholder'));

				foreach ( $get_selects as $get_select ) {
					$select_classes = $get_select->getAttribute( 'class' );
					$select_id = $get_select->getAttribute( 'id' );
					$select_classes .= ' mdc-select__native-control';
					if ( $contains_error ) {
						$select_classes .= ' mdc-text-field--invalid';
					}
					$get_select->setAttribute( 'class', $select_classes );
				}

				$div_classes .= ' mdc-select mdc-text-field--no-label';
				$div_classes .= $type === 'outlined' ? ' mdc-select--outlined':'';

				$div->setAttribute( 'class', $div_classes );

				if( $type === 'outlined' ) {

					$notched_outline = $dom->createElement( 'div' );
					$notched_outline->setAttribute( 'class', 'mdc-notched-outline' );

					$notched_outline_leading = $dom->createElement( 'div' );
					$notched_outline_leading->setAttribute( 'class', 'mdc-notched-outline__leading' );
					
					$notched_outline_notch = $dom->createElement( 'div' );
					$notched_outline_notch->setAttribute( 'class', 'mdc-notched-outline__notch' );

					$notched_outline_trailing = $dom->createElement( 'div' );
					$notched_outline_trailing->setAttribute( 'class', 'mdc-notched-outline__trailing' );
					$div->appendChild( $select_icon );
					$div->appendChild( $notched_outline );
					

					$notched_outline->appendChild( $notched_outline_leading );
					$notched_outline->appendChild( $notched_outline_notch );
					$notched_outline->appendChild( $notched_outline_trailing );

					// $label->setAttribute('class', $label->getAttribute( 'class' ) . $add_label_classes );
					// $notched_outline_notch->appendChild( $label );
				}
				// $div->setAttribute( 'data-mdc-auto-init', 'MDCTextField' );

				//get the first label
				// $get_label = $dom->getElementsByTagName( 'label' )->item( 0 );

				// $get_label->setAttribute( 'class', $get_label->getAttribute( 'class' ) . ' mdc-floating-label  mdc-floating-label--float-above' );
				// //move label to be the last child of div
				// $div->appendChild( $get_label );

				//create a new div
                if ($type === 'filled') {
					
                    $bottom_line_div = $dom->createElement('span');
					$bottom_line_div->setAttribute('class', 'mdc-line-ripple');
					$div->appendChild( $select_icon );
                    $div->appendChild($bottom_line_div);
                }
				//$content = utf8_decode($dom->saveHTML($dom->documentElement));
			}
		}

		$content = utf8_decode( $dom->saveHTML( $dom->documentElement ) );
	}

}
