<?php

namespace BwWinnersGlobalSite\Brand_Update;

if ( ! class_exists( __NAMESPACE__ . '\Update_Form' ) ) {

	class Update_Form {
		
		public $form_page_name = 'brand-update';
		public $survey_page_name = 'brand-update-survey';

		public function __construct () {

			// Hooking up methods as callbacks
			add_action( 'init', array( $this, 'custom_rewrite_rule' ) );

			add_filter( 'query_vars', array( $this, 'add_custom_query_vars' ) );

			add_shortcode( 'bw-v2-brand-form-brand-title', array( $this, 'bw_brand_title' ) );

			add_filter( 'gform_field_value_brand_id', array( $this, 'set_brand_id_field' ) );

			add_filter( 'gform_field_value_brand_name', array( $this, 'autopopulate_brand_name' ) );

			add_filter( 'gform_field_value_brand_url', array( $this, 'autopopulate_brand_url' ) );

			add_filter( 'gform_field_value_brand_facebook', array( $this, 'autopopulate_brand_facebook' ) );

			add_filter( 'gform_field_value_brand_instagram', array( $this, 'autopopulate_brand_instagram' ) );

			add_filter( 'gform_field_value_brand_linkedin', array( $this, 'autopopulate_brand_linkedin' ) );
			
			add_filter( 'gform_field_value_brand_twitter', array( $this, 'autopopulate_brand_twitter' ) );
		}

		public function custom_rewrite_rule () {
			add_rewrite_rule(
				'^' . $this->form_page_name . '/(\d+)/?',
				'index.php?pagename=' . $this->form_page_name . '&brand-id=$matches[1]', 
				'top'
			);
			
			add_rewrite_rule(
				'^' . $this->survey_page_name . '/(\d+)/?',
				'index.php?pagename=' . $this->survey_page_name . '&brand-id=$matches[1]', 
				'top'
			);
		}

		public function add_custom_query_vars ( $query_vars ) {
			$query_vars[] = 'brand-id';
			return $query_vars;
			
		}

		public function bw_brand_title ( $atts = [], $content = null ) {

			global $bw_winners_global_site;

			$attributes = shortcode_atts( array(
				'brand_id' => get_query_var( 'brand-id', false ),
			), $atts );

			$brand_id = intval( $attributes['brand_id'] );

			$brand = $bw_winners_global_site->entities->get_brand( $brand_id );

			if ( $brand ) {
				return $brand['name'];
			}

			return $content;
		}


		public function set_brand_id_field ( $value ) {
			$brand_id = get_query_var( 'brand-id', false );
			return $brand_id ? $brand_id : $value;
		}

		public function autopopulate_brand_name ( $value ) {

			global $bw_winners_global_site;

			$brand_id = get_query_var( 'brand-id', false );

			$brand = $bw_winners_global_site->entities->get_brand( intval( $brand_id ) );

			if ( $brand ) {
				return $brand['name'];
			}

			return $value;
		}
		
		public function autopopulate_brand_url ( $value ) {

			global $bw_winners_global_site;

			$brand_id = get_query_var( 'brand-id', false );

			if ( $brand_id ) {

				$field = get_field( 'brand_link', $brand_id );

				if ( $field ) {
					return $field;
				}
			}

			return $value;
		}

		public function autopopulate_brand_facebook ( $value ) {

			global $bw_winners_global_site;

			$brand_id = intval( get_query_var( 'brand-id', false ) );

			if ( $brand_id ) {

				$field = get_field( 'facebook', $brand_id );

				if ( $field ) {
					return $field;
				}
			}

			return $value;
		}

		public function autopopulate_brand_instagram ( $value ) {

			global $bw_winners_global_site;

			$brand_id = intval( get_query_var( 'brand-id', false ) );

			if ( $brand_id ) {

				$field = get_field( 'instagram', $brand_id );

				if ( $field ) {
					return $field;
				}
			}

			return $value;
		}

		public function autopopulate_brand_linkedin ( $value ) {

			global $bw_winners_global_site;

			$brand_id = intval( get_query_var( 'brand-id', false ) );

			if ( $brand_id ) {

				$field = get_field( 'linkedin', $brand_id );

				if ( $field ) {
					return $field;
				}
			}

			return $value;
		}
		public function autopopulate_brand_twitter ( $value ) {

			global $bw_winners_global_site;

			$brand_id = intval( get_query_var( 'brand-id', false ) );

			if ( $brand_id ) {

				$field = get_field( 'twitter', $brand_id );

				if ( $field ) {
					return $field;
				}
			}

			return $value;
		}
	}
}
