<?php
namespace Datacake\Core\Widgets;

use Datacake\Core\Models\IspCity;
use Datacake\Core\Utils;
use \WP_Widget;

class CitySEOWidget extends WP_Widget  {
    public $fieldTitle = "lpnet_cid_title";
    public $fieldText = "lpnet_cid_text";
	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'lpnet_cidades', // Base ID
			esc_html__( 'Datacake - SEO Cidades', 'text_domain' ), // Name
			array( 'description' => esc_html__( 'Texto automatico SEO das cidades', 'text_domain' ), ) // Args
		);
	}
	/*
	<div id="text-5" class="widget widget_text"><h4 class="h-widget">About</h4>			<div class="textwidget">We are a growing company with a focus on the future. Our mission is to change the way people think about widgets and have fun while doing it. <a href="#holder">Read More</a>.</div>
		</div>
		*/

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {
		echo $args['before_widget'];
        $cityCookie = false;
        $this->utils = new Utils();
        $cityCookie = $this->utils->getCity();

        $Lpnet = new Lpnet;
        $city = $cityCookie->getCity();
        if(empty($city)){
            $cidade = 'Sua Cidade';
        }else{
            $cidade = $city->name;
        }
        if ( ! empty( $instance[$this->fieldTitle] ) ) {
		    $title = $instance[$this->fieldTitle];
		    $title = str_replace('[cidade]',$cidade,$title);
		    echo '<h4 class="h-widget">'.$title.'</h4>';
		}
		if ( ! empty( $instance[$this->fieldText] ) ) {
		    $texto = $instance[$this->fieldText];
		    $texto = str_replace('[cidade]',$cidade,$texto);
		    echo '<div class="textwidget">'.$texto.'</div>';
		}
	
		echo $args['after_widget'];
	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {
	  
	    $title = @$instance[$this->fieldTitle];
	    $text = @$instance[$this->fieldText];
	    
	    $fieldTitleId = $this->get_field_id( $this->fieldTitle );
	    $fieldTitleName = $this->get_field_name( $this->fieldTitle );
	    
	    $fieldTextId = $this->get_field_id( $this->fieldText );
	    $fieldTextName = $this->get_field_name( $this->fieldText );
	    
		//$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
		//$text = ! empty( $instance['text'] ) ? $instance['text'] : esc_html__( 'New Text', 'text_domain' );
		?>
		<p>
		    <label for="<?php echo $fieldTitleId; ?>"><?php _e('Title', 'text_domain'); ?>:</label>
			<input type="text" name="<?php echo $fieldTitleName; ?>" id="<?php echo $fieldTitleId; ?>" value="<?php echo $title; ?>" class="widefat">
		</p>
		<p>
		    <label for="<?php echo $fieldTextId; ?>"><?php _e('Content', 'text_domain'); ?>:</label>
		    <textarea rows="16" cols="20" type="text" name="<?php echo $fieldTextName; ?>" id="<?php echo $fieldTextId; ?>" class="widefat" ><?php echo $text; ?></textarea>
		    <info>Utilizar [cidade] para trocar pelo nome da cidade</info>
		</p>
		<?php 
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update( $new_instance, $old_instance ) {
	  
		 return $new_instance;
	}
}