<?php

/**
 * This is the model class for table "googlemaps_marker".
 *
 * The followings are the available columns in table 'googlemaps_marker':
 * @property integer $id
 * @property string $title
 * @property string $animation
 * @property integer $clickable
 * @property string $cursor
 * @property integer $draggable
 * @property string $icon
 * @property double $latitude
 * @property double $longitude
 * @property integer $map_id
 *
 * The followings are the available model relations:
 * @property GoogleMap $map
 */
class GoogleMapMarker extends CActiveRecord {

    const ANIMATION_BOUNCE = 'BOUNCE';
    const ANIMATION_DROP = 'DROP';

    public $icon_image;
    public $icon_select = 'red-dot.png';

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'googlemaps_marker';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
        return array(
            array('title, latitude, longitude, map_id', 'required'),
            array('clickable, draggable, map_id', 'numerical', 'integerOnly' => true),
            array('latitude, longitude', 'numerical'),
            array('title, icon', 'length', 'max' => 200),
            array('animation', 'length', 'max' => 7),
            array('cursor', 'length', 'max' => 20),
            array('icon_select', 'safe'),
            array('icon_image', 'file', 'types' => 'jpg,jpeg,png,gif', 'maxSize' => 1024 * 1024 * 15 /* 15MB */, 'allowEmpty' => true, 'safe' => true),
            // The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
            array('id, title, animation, clickable, cursor, draggable, icon, latitude, longitude, map_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
        return array(
            'map' => array(self::BELONGS_TO, 'GoogleMap', 'map_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => Yii::t('admin', 'ID'),
            'title' => Yii::t('admin', 'Title'),
            'animation' => Yii::t('admin', 'Animation'),
            'clickable' => Yii::t('admin', 'Clickable'),
            'cursor' => Yii::t('admin', 'Cursor'),
            'draggable' => Yii::t('admin', 'Draggable'),
            'icon' => Yii::t('admin', 'Icon'),
            'latitude' => Yii::t('admin', 'Latitude'),
            'longitude' => Yii::t('admin', 'Longitude'),
            'map_id' => Yii::t('admin', 'Map'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
// @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('animation', $this->animation, true);
        $criteria->compare('clickable', $this->clickable);
        $criteria->compare('cursor', $this->cursor, true);
        $criteria->compare('draggable', $this->draggable);
        $criteria->compare('icon', $this->icon, true);
        $criteria->compare('latitude', $this->latitude);
        $criteria->compare('longitude', $this->longitude);
        $criteria->compare('map_id', $this->map_id);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return GoogleMapMarker the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function getAnimations() {
        return array(
            self::ANIMATION_BOUNCE => Yii::t('admin', 'Bounce'),
            self::ANIMATION_DROP => Yii::t('admin', 'Drop'),
        );
    }

    public static function getCursors() {
        return array(
            'crosshair' => 'crosshair',
            'default' => 'default',
            'pointer' => 'pointer',
            'move' => 'move',
            'e-resize' => 'e-resize',
            'ne-resize' => 'ne-resize',
            'nw-resize' => 'nw-resize',
            'n-resize' => 'n-resize',
            'se-resize' => 'se-resize',
            'sw-resize' => 'sw-resize',
            's-resize' => 's-resize',
            'w-resize' => 'w-resize',
            'text' => 'text',
            'wait' => 'wait',
            'help' => 'help',
            'progress' => 'progress',
        );
    }

    public static function getDefaultIcons() {
        return array(
            'blue-dot.png' => 'Blue Dot',
            'red-dot.png' => 'Red Dot',
            'green-dot.png' => 'Green Dot',
            'ltblue-dot.png' => 'Ltblue Dot',
            'yellow-dot.png' => 'Yellow Dot',
            'purple-dot.png' => 'Purple Dot',
            'pink-dot.png' => 'Pink Dot',
            'blue.png' => 'Blue Solid Dot',
            'red.png' => 'Red Solid Dot',
            'green.png' => 'Green Solid Dot',
            'lightblue.png' => 'Light Blue Solid Dot',
            'yellow.png' => 'Yellow Solid Dot',
            'purple.png' => 'Purple Solid Dot',
            'pink.png' => 'Pink Solid Dot',
            'blue-pushpin.png' => 'Blue Pushpin',
            'red-pushpin.png' => 'Red Pushpin',
            'grn-pushpin.png' => 'Grn Pushpin',
            'ltblu-pushpin.png' => 'Ltblu Pushpin',
            'ylw-pushpin.png' => 'Ylw Pushpin',
            'purple-pushpin.png' => 'Purple Pushpin',
            'pink-pushpin.png' => 'Pink Pushpin',
            'restaurant.png' => 'Restaurant',
            'coffeehouse.png' => 'Coffee House',
            'bar.png' => 'Bar',
            'snack_bar.png' => 'Snack Bar',
            'man.png' => 'Man',
            'woman.png' => 'Woman',
            'wheel_chair_accessible.png' => 'Wheel Chair Accessible',
            'parkinglot.png' => 'Parkinglot',
            'cabs.png' => 'Cabs',
            'bus.png' => 'Bus',
            'truck.png' => 'Truck',
            'rail.png' => 'Rail',
            'plane.png' => 'Plane',
            'ferry.png' => 'Ferry',
            'helicopter.png' => 'Helicopter',
            'subway.png' => 'Subway',
            'tram.png' => 'Tram',
            'info.png' => 'Info',
            'info_circle.png' => 'Info Circle',
            'flag.png' => 'Flag',
            'rainy.png' => 'Rainy',
            'water.png' => 'Water',
            'snowflake_simple.png' => 'Snowflake Simple',
            'marina.png' => 'Marina',
            'fishing.png' => 'Fishing',
            'sailing.png' => 'Sailing',
            'swimming.png' => 'Swimming',
            'ski.png' => 'Ski',
            'tree.png' => 'Tree',
            'campfire.png' => 'Campfire',
            'picnic.png' => 'Picnic',
            'campground.png' => 'Campground',
            'rangerstation.png' => 'Ranger Station',
            'toilets.png' => 'Toilets',
            'POI.png' => 'POI',
            'hiker.png' => 'Hiker',
            'cycling.png' => 'Cycling',
            'motorcycling.png' => 'Motorcycling',
            'horsebackriding.png' => 'Horseback Riding',
            'sportvenue.png' => 'Sport Venue',
            'golfer.png' => 'Golfer',
            'trail.png' => 'Trail',
            'shopping.png' => 'Shopping',
            'movies.png' => 'Movies',
            'grocerystore.png' => 'Grocery Store',
            'convienancestore.png' => 'Convienance Store',
            'arts.png' => 'Arts',
            'homegardenbusiness.png' => 'Home Garden Business',
            'electronics.png' => 'Electronics',
            'mechanic.png' => 'Mechanic',
            'gas.png' => 'Gas',
            'realestate.png' => 'Real Estate',
            'salon.png' => 'Salon',
            'dollar.png' => 'Dollar',
            'euro.png' => 'Euro',
            'yen.png' => 'Yen',
            'firedept.png' => 'Firedept',
            'hospitals.png' => 'Hospitals',
            'lodging.png' => 'Lodging',
            'phone.png' => 'Phone',
            'caution.png' => 'Caution',
            'earthquake.png' => 'Earthquake',
            'fallingrocks.png' => 'Falling Rocks',
            'postoffice-us.png' => 'Post Office US',
            'police.png' => 'Police',
            'sunny.png' => 'Sunny',
            'partly_cloudy.png' => 'Partly Cloudy',
            'volcano.png' => 'Volcano',
            'camera.png' => 'Camera',
            'webcam.png' => 'Webcam',
        );
    }

    public function getImagePathUrl() {
        $url = NULL;
        if ($this->icon) {
            if (in_array($this->icon, array_keys(self::getDefaultIcons()))) {
                $url = 'https://maps.gstatic.com/mapfiles/ms2/micons/';
            } else {
                $url = Yii::app()->params['uploadUrl'] . get_class($this) . '/';
            }
        }
        return $url;
    }

    public function getOptions($mapId) {
        $options = array(
            'title' => $this->title,
            'animation' => $this->animation ? new CJavaScriptExpression("google.maps.Animation.{$this->animation}") : false,
            'clickable' => (boolean) $this->clickable,
            'cursor' => $this->cursor,
            'draggable' => (boolean) $this->draggable,
            'icon' => $this->getImagePathUrl() . $this->icon,
            'position' => new CJavaScriptExpression("new google.maps.LatLng({$this->latitude}, {$this->longitude})"),
            'map' => new CJavaScriptExpression($mapId),
        );


        return CJavaScript::encode($options);
    }

    public function getPosition() {
        return new CJavaScriptExpression("new google.maps.LatLng({$this->latitude}, {$this->longitude})");
    }

}
