<?php
/**
 * @copyright	Copyright (c) 2016 AmyTheme (http://www.amytheme.com). All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

defined( 'ABSPATH' ) or die;

class Amy_Movie_Widget_Module extends AmyFramework_Widget {

	function __construct() {
		$widget_ops		= array(
			'classname'		=> 'amy-widget-module',
			'description'	=> esc_html__( 'Show Utilities Module into Widget.', 'amy-movie' ),
		);

		$control_ops	= array(
			'width'			=> 400,
			'height'		=> 350,
		);

		parent::__construct( 'amy_movie_widget_module', esc_html__( '+ Amy Movie Module', 'amy-movie' ), $widget_ops, $control_ops );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title		= empty( $instance['title'] ) ? '' : $instance['title'];
		$type 		= empty( $instance['type'] ) ? '' : $instance['type'];
		$address 	= empty( $instance['address'] ) ? '' : $instance['address'];
		$email 		= empty( $instance['email'] ) ? '' : $instance['email'];
		$phone 		= empty( $instance['phone'] ) ? '' : $instance['phone'];
		$logo 		= empty( $instance['logo'] ) ? '' : $instance['logo'];
		$about_text = empty( $instance['about_text'] ) ? '' : $instance['about_text'];
		$banner 	= empty( $instance['banner'] ) ? '' : $instance['banner'];
		$link 		= empty( $instance['link'] ) ? '' : $instance['link'];
		$class 		= empty( $instance['class'] ) ? '' : $instance['class'];

		echo $before_widget;

		$output = '<div class="amy-widget amy-widget-module ' . esc_attr( $type ) . ' ' . esc_attr( $class ) . '">';

		if ( ! empty( $title ) ) {
			$output .= '<h4 class="amy-title amy-widget-title">' . esc_html( $title ) . '</h4>';
		}

		switch ( $type ) {
			case 'advertising':
				if ( $banner != '' ) {
					$output .= '<a href="' . esc_url( $link ) . '" target="_blank">';
					$output .= wp_get_attachment_image( $banner, 'full' );
					$output .= '</a>';
				}

				break;

			case 'contact':
				if ( $address != '' ) {
					$output .= '<div class="address"><i class="fa fa-location-arrow" aria-hidden="true"></i>' . esc_html( $address ) . '</div>';
				}

				if ( $email != '' ) {
					$output .= '<div class="email"><i class="fa fa-envelope-o" aria-hidden="true"></i>' . esc_html( $email ) . '</div>';
				}

				if ( $phone != '' ) {
					$output .= '<div class="phone"><i class="fa fa-phone" aria-hidden="true"></i>' . esc_html( $phone ) . '</div>';
				}

				break;

			case 'social':
				$output .= amy_movie_social_list();

				break;

			case 'about':
				if ( $logo != '' ) {
					$output .= '<div class="footer-logo"><a href="' . esc_url( home_url( '/' ) ) . '">';
					$output .= wp_get_attachment_image( $logo, 'full' );
					$output .= '</a></div>';
				}

				if ( $about_text != '' ) {
					$output .= '<div class="summary">' . $about_text . '</div>';
				}

				break;
		}

		$output .= '</div>';

		echo $output;

		echo $after_widget;
	}

	function get_options() {
		return array(
			array(
				'id'		=> 'title',
				'type'		=> 'text',
				'title'		=> esc_html__( 'Title', 'amy-movie' ),
			),

			array(
				'id'		=> 'type',
				'type'		=> 'select',
				'title'		=> esc_html__( 'Type', 'amy-movie' ),
				'options'			=> array(
					'advertising'	=> esc_html__( 'Advertising', 'amy-movie' ),
					'contact'		=> esc_html__( 'Contact', 'amy-movie' ),
					'social'		=> esc_html__( 'Social List', 'amy-movie' ),
					'about'			=> esc_html__( 'About', 'amy-movie' ),
				),
			),

			array(
				'id'		=> 'banner',
				'type'		=> 'image',
				'title'		=> esc_html__( 'Banner', 'amy-movie' ),
				'dependency'	=> array( 'type', '==', 'advertising' ),
			),

			array(
				'id'				=> 'link',
				'type'				=> 'text',
				'title'				=> esc_html__( 'Banner URL', 'amy-movie' ),
				'multilang'			=> true,
				'attributes'		=> array(
					'placeholder'		=> 'http://',
				),
				'dependency'	=> array( 'type', '==', 'advertising' ),
			),

			array(
				'id'			=> 'address',
				'type'			=> 'text',
				'title'			=> esc_html__( 'Address', 'amy-movie' ),
				'dependency'	=> array( 'type', '==', 'contact' ),
			),

			array(
				'id'			=> 'email',
				'type'			=> 'text',
				'title'			=> esc_html__( 'Email', 'amy-movie' ),
				'dependency'	=> array( 'type', '==', 'contact' ),
			),

			array(
				'id'			=> 'phone',
				'type'			=> 'text',
				'title'			=> esc_html__( 'Phone', 'amy-movie' ),
				'dependency'	=> array( 'type', '==', 'contact' ),
			),

			array(
				'id'		=> 'logo',
				'type'		=> 'image',
				'title'		=> esc_html__( 'Logo', 'amy-movie' ),
				'dependency'	=> array( 'type', '==', 'about' ),
			),

			array(
				'id'			=> 'about_text',
				'type'			=> 'textarea',
				'title'			=> esc_html__( 'Summary', 'amy-movie' ),
				'dependency'	=> array( 'type', '==', 'about' ),
			),

			array(
				'id'		=> 'class',
				'type'		=> 'text',
				'title'		=> esc_html__( 'Extra Class', 'amy-movie' ),
			),
		);
	}
}

register_widget( 'Amy_Movie_Widget_Module' );
