define(['jquery', 'underscore', 'backbone', 'text!modules/photos/photosViewTemplate.html', 'modules/base/baseView',
    'controls/window/window', 'jqx'],
        function($, _, Backbone, photosViewTemplate, BaseView, Window) {

            var PhotosView = BaseView.extend({
                dataAdapter: undefined,
                //initialize template
                template: _.template(photosViewTemplate),
                initialize: function() {
                    PhotosView.__super__.initialize.call(this);
                    //this.header.title = "pippo";
                },
                checkPhotos: function(row){
                    var dataRecord = $("#gridPhotos").jqxGrid('getrowdata', row);
                },
                //render the content into div of view
                render: function() {
                    //this.header.title = "pippo";
                    PhotosView.__super__.render.call(this);
                    var self = this;

                    var window = new Window({
                        title: "Foto",
                        width: "850px",
                        resizable: false,
                        height: "510px",
                        content: "<p><h3>Ricette</h3><div id=\"gridRicette\"></div><h3>Foto</h3><div id=\"gridPhotos\"></div></p>",
                        initContentFn: function() {
                            // prepare the data
                            self.dataAdapter = new $.jqx.dataAdapter({
                                datatype: "jsonp",
                                datafields: [
                                    {name: 'ricetta_id', type: 'int'},
                                    {name: 'titolo', type: 'string'},
                                    {name: 'autore', type: 'string'},
                                    {name: 'num_img', type: 'int'},
                                    {name: 'num_img_pub', type: 'int'},
                                    {name: 'all_published', type: 'bool'}
                                ],
                                id: 'ricetta_id',
                                url: myManifest.Settings.DefaultURL + "/backend/photos",
                                data: {
                                }
                            });
                            $("#gridRicette").jqxGrid({
                                width: "100%",
                                height: "200px",
                                source: self.dataAdapter,
                                filterable: true,
                                autoshowfiltericon: true,
                                columns: [
                                    {text: 'Titolo', dataField: 'titolo', width: "400px"},
                                    {text: 'Inviate', dataField: 'num_img', width: "65px"},
                                    {text: 'All Published', editable: false, dataField: 'all_published', width: "70px",
                                        cellsrenderer: function(index, datafield, value, defaultvalue, column, rowdata) {
                                            var total = (parseFloat(rowdata.num_img) === parseFloat(rowdata.num_img_pub));
                                            return "<div style='margin: 4px;' class='jqx-center-align'>" + total + "</div>";
                                        }
                                    }
                                ]
                            });
                            
                            var photoFields = [
                                {name: 'id_photo', type: 'int'},
                                {name: 'type_format', type: 'string'},
                                {name: 'from_profile_id', type: 'string'},
                                {name: 'uploaded_date', type: 'datetime'},
                                {name: 'profile_name', type: 'string'},
                                {name: 'published', type: 'bool'}
                            ];
                            
                            var photoAdapter = new $.jqx.dataAdapter({
                                datatype: "jsonp",
                                datafields: photoFields,
                                id: 'id_photo',
                                url: myManifest.Settings.DefaultURL + "/backend/ricetta/0/photos",
                                data: {
                                }
                            });
                            
                            $("#gridPhotos").jqxGrid({
                                width: "100%",
                                height: "150px",
                                source: photoAdapter,
                                rowsheight: 100,
                                //filterable: true,
                                autoshowfiltericon: true,
                                columns: [
                                    {text: 'Autore', dataField: 'profile_name', width: "170px"},
                                    {text: 'Image', dataField: 'image', width: "210px",
                                        cellsrenderer: function(index, datafield, value, defaultvalue, column, rowdata) {
                                            return '<img style="margin-left: 5px;width: 200px;" src="' + myManifest.Settings.DefaultURL + '/api/photos/thumbnail/' + rowdata.id_photo + '"/>';
                                        }
                                    },
                                    {text: 'Published', editable: false, dataField: 'published', width: "70px",
                                        cellsrenderer: function(index, datafield, value, defaultvalue, column, rowdata) {
                                            var valueBool = rowdata.published;
                                            return "<div style='margin: 4px;' class='jqx-center-align'>" + valueBool + "</div>";
                                        }
                                    },
                                    {text: '', datafield: 'FullView', columntype: 'button', width: '10%',
                                        cellsrenderer: function() {
                                            return "View Full";
                                        },
                                        buttonclick: function(row) {
                                            
                                        }
                                    },
                                    {text: '', datafield: 'Publish', columntype: 'button', width: '10%',
                                        cellsrenderer: function() {
                                            return "Publish";
                                        },
                                        buttonclick: function(row) {
                                            if(row.published === false)
                                            {}
                                        }
                                    }
                                ]
                            });

                            $('#gridRicette').on('rowselect', function(event)
                            {
                                var ricetta_id = event.args.row.ricetta_id;
                                var photoAdapter = new $.jqx.dataAdapter({
                                    datatype: "jsonp",
                                    datafields: photoFields,
                                    id: 'id_photo',
                                    url: myManifest.Settings.DefaultURL + "/backend/ricetta/" + ricetta_id + "/photos",
                                    data: {
                                    }
                                });
                                
                                $("#gridPhotos").jqxGrid({ source: photoAdapter });
                            });
                        }
                    });
                    window.id = "photos";
                    this.$el.append(this.template());
                    this.$el.find("#content").append(window.$el);
                    window.render();

                    return this;
                }
            });
            return PhotosView;
        });