/**
 * Created by ajax on 2015/11/30.
 */
define(function (require, exports, module) {
    function Blogs() {
        this.obj = null;        // parse Blogs obj
        this.query = null;      // parse query obj
        this.pageSize = 10;     // 单页大小
        this.page = 1;          // 分页
        this.id = "";         // 获取单行 blog
        this.selectArr = ["objectId", "title", "desc", "stamps", "types",
                        "date_entered", "view", "createdAt", "updatedAt", "hasContent"];
        this.map = {
            "b1e": "前端",
            "b2e": "后台",
            "b3e": "算法",
            "b4e": "乱扯",
            "b5e": "闲聊",
            "b6e": "吐槽",
            "b11e": "HTML",
            "b12e": "CSS",
            "b13e": "JS",
            "b21e": "PHP",
            "b22e": "MySQL"
        };
        // database table column
        this.title = "";
        this.types = "";
        this.desc = "";
        this.stamps = "";
        this.content = "";
        this.hasContent = false;
        this.date_entered = null;
        this.is_delete = false;
    }
    Blogs.prototype = {
        getObj : function () {
            if (this.obj) return this.obj;
            this.obj = new Parse.Object.extend("blogs");
            return this.obj;
        },
        getQuery : function () {
            if (this.query) return this.query;
            this.query = new Parse.Query(this.getObj());
            return this.query;
        },
        getTotalNum : function (success, error) {
            var query = this.getQuery();
            query.containedIn("is_delete", [false, undefined, '']);
            query.count({
                success : function (total) {
                    success && success(total);
                },
                error : function (err) {
                    error && error(err);
                }
            });
        },
        getOne : function (success, error) {
            var Blog = Parse.Object.extend("blogs");
            var query = new Parse.Query(Blog);
            query.get(this.id, {
                success: function (item) {
                    success && success(item);
                },
                error: function (err) {
                    error && error(err);
                }
            });
        },
        getPage : function (success, error) {
            var query = this.getQuery();
            var that = this;
            query.limit(this.pageSize);
            query.skip((this.page - 1) * this.pageSize);
            query.select(this.selectArr.join(","));
            query.descending("date_entered");
            query.find({
                success : function (results) {
                    var json = {},
                        prop = {};
                    json.results = [];
                    for (var i = 0; i < results.length; i++) {
                        prop = {};
                        _.each(that.selectArr, function (item) {
                            prop[item] = encodeURIComponent(results[i].get(item));
                        });
                        prop["objectId"] = results[i].id;
                        json.results.push(prop);
                    }
                    success && success(JSON.stringify(json));
                },
                error : function (err) {
                    error && error(err);
                }
            });
        },
        save : function (success, error) {
            var user = Parse.User.current(),
                acl = new Parse.ACL(user);
            if (!user) {
                alert("未登录");
                return false;
            }
            console.log("I am uploading...");
            var Blog,
                blog;
            Blog = Parse.Object.extend("blogs");
            blog = new Blog();
            var data = {
                title: this.title,
                types: this.types,
                desc: this.desc,
                content: this.content,
                stamps: this.stamps,
                hasContent : this.hasContent,
                is_delete : this.is_delete
            };
            if (this.id) {  // 修改
                blog.id = this.id;
            } else {        // new add
                acl.setPublicReadAccess(true);
                blog.setACL(acl);
                data.date_entered = this.date_entered || (new Date());
            }
            blog.save(data, {
                success: function (res) {
                    success && success(res);
                },
                error: function (err) {
                    error && error(err);
                }
            });
        }
    };


    module.exports = {
        Blogs : Blogs
    };
});