<?php

namespace WoWPress\Frontend\Widgets;

class BossKills extends Widget
{

	protected $template_path = "boss_kills.php";
	protected $title = "Boss-Kills";
	protected $base_id = "wowpress_boss_kills";
	public $name = "Boss-Kills";
	protected $restricted = false;


	public function getBossList($instance){
		$out = [];
		foreach($instance['bosses'] as $boss){
			if(!empty($boss['name'])){
				$out[] = $boss;
			}
		} 

		return $out;
	}

	public function getCompletePercent($instance){
		$bosses = $this->getBossList($instance);
		$done = 0;
		foreach($bosses as $boss){
			if(!empty($boss['date'])){
				$done++;
			}
		}

	
		return ($done/count($bosses))*100;
	}

	public function form($instance)
	{
		$raid_name = !empty($instance['raid_name']) ? $instance['raid_name'] : ""; ?>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id("raid_name")); ?>"><?php echo esc_html__('Raid-Name', 'wowpress'); ?></label>
			<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id("raid_name")); ?>" name="<?php echo esc_attr($this->get_field_name("raid_name")); ?>" value="<?php echo esc_attr($raid_name); ?>">
		</p>
		<?php
		for ($i = 0; $i <= 15; $i++) {
			$boss_name =	!empty($instance['bosses'][$i]['name']) ? $instance['bosses'][$i]['name'] : "";
			$boss_kill =	!empty($instance['bosses'][$i]['date']) ? $instance['bosses'][$i]['date'] : "";
		?>
			<p>
				<label for="<?php echo esc_attr($this->get_field_id("bosses[$i][name]")); ?>"><?php echo esc_html__('Boss-Name', 'wowpress'); ?></label>
				<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id("bosses[$i][name]")); ?>" name="<?php echo esc_attr($this->get_field_name("bosses[$i][name]")); ?>" value="<?php echo esc_attr($boss_name); ?>">
			</p>
			<p>
				<label for="<?php echo esc_attr($this->get_field_id("bosses[$i][date]")); ?>"><?php echo esc_html__('Firstkill', 'wowpress'); ?></label>
				<input type="date" class="widefat" id="<?php echo esc_attr($this->get_field_id("bosses[$i][date]")); ?>" name="<?php echo esc_attr($this->get_field_name("bosses[$i][date]")); ?>" value="<?php echo esc_attr($boss_kill); ?>">
			</p>
<?php
		}
	}

	public function update($new_instance, $old_instance)
	{
		$instance          = array();
		$instance['raid_name'] = (!empty($new_instance['raid_name'])) ? strip_tags($new_instance['raid_name']) : '';

		for ($i = 0; $i <= 15; $i++) {
			$instance['bosses'][$i]['name'] = (!empty($new_instance['bosses'][$i]['name'])) ? strip_tags($new_instance['bosses'][$i]['name']) : '';
			$instance['bosses'][$i]['date'] = (!empty($new_instance['bosses'][$i]['date'])) ? strip_tags($new_instance['bosses'][$i]['date']) : '';
		}

		return $instance;
	}
}
