import Ember from 'ember';

export default Ember.Component.extend({
    session: Ember.inject.service(),
    depth: 0,   // hloubka vlakna v diskuzi
    max_depth: 3,   // nejvetsi hloubka, ve ktere jeste zanorovat

    edit_progress: false,
    edit_error: "",
    react_progress: false,
    react_error: "",
    react_error_show: false,

    depth_inc: Ember.computed("depth", function(){
        return this.get("depth") + 1;
    }),

    // jestli byl presahnut limit zanorovani
    depth_exceeded: Ember.computed("depth", "max_depth", function(){
        return this.get("depth") >= this.get("max_depth");
    }),

    mark_as_read: function() {
        var post = this.get("model");
        while (post) {
            post.set("is_new", false);
            post = post.get("parent");
        }
    },

    actions: {
        react: function() {
            this.set("is_reacting", !this.get("is_reacting"));
            this.mark_as_read();
            this.set("content_error", undefined);
            this.set("response_text", "");
        },
        send: function() {
            var self = this;
            this.set("react_error", "");

            if(!this.get("response_text")) {
                this.set("react_error", "Nelze odeslat prázdný příspěvek");
                this.set("react_error_show", true);
                return;
            }

            this.set("react_progress", true);

            var post = this.get("store").createRecord("post", {
                body: self.get("response_text"),
                parent: self.get("model"),
                thread: self.get("model.thread"),
                temporary: true
            });

            post.save().then(function() {
                post.set("temporary", false);
                self.set("react_progress", false);
                self.set("is_reacting", false);
                self.set("response_text", "");
                Ember.run.later(self, function() {
                    MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
                }, 500);
            }, function(resp) {
                // fail
                post.destroyRecord();
                self.set("react_progress", false);
                var e = "Nepodařilo se uložit příspěvek! Pokud si myslíš, že chyba není na tvé straně, kontaktuj organizátora.<br>" + resp.message;
                if (resp.errors[0]) { e += "<br>" + resp.errors[0].status  + " : " + resp.errors[0].title; }
                self.set("react_error", e);
            });
        },
        delete: function() {
            if (!confirm("Opravdu smazat příspěvek a všechny reakce na něj?")) {
                return;
            }
            this.get("model").deleteRecord();
            this.get("model").save();
        },
        edit: function() {
            this.set("content_bak", this.get("model.body"));
            this.set("is_editing", true);
            this.set("edit_error", undefined);
            this.set("edit_progress", undefined);
        },
        cancel: function() {
            this.set("model.body", this.get("content_bak"));
            this.set("is_editing", false);
            Ember.run.later(this, function() {
                MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
            }, 500);
        },
        save: function() {
            var self = this;
            this.set("edit_progress", true);
            this.set("edit_error", "");

            this.get("model").save().then(function() {
                    self.set("is_editing", false);
                    self.set("edit_progress", false);
                    Ember.run.later(self, function() {
                        MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
                    }, 500);
                },
                function(resp) {
                    self.set("edit_progress", false);
                    var e = "Nepodařilo se uložit příspěvek! Pokud si myslíš, že chyba není na tvé straně, kontaktuj organizátora.<br>" + resp.message;
                    if (resp.errors[0]) { e += "<br>" + resp.errors[0].status  + " : " + resp.errors[0].title; }
                    self.set("edit_error", e);
                }
            );
        }
    },
    allow_edit: Ember.computed("session.current_user", "model.author", "model.thread.year.sealed", function() {
        return this.get("session.current_user.organisator") && !this.get("model.thread.year.sealed");
    }),
    allow_delete: Ember.computed("session.current_user", "model.thread.year.sealed", function() {
       return this.get("session.current_user.organisator") && !this.get("model.thread.year.sealed");
    }),
    is_reacting: false,
    content_error: undefined
});

@@ -654,32 +654,36 @@
 _read: function(
+post
 ) %7B%0D%0A        var
@@ -687,135 +687,192 @@
 var 
-post
+self
  = this
-.get(%22model%22);%0D%0A        while (post) %7B%0D%0A            post.set(%22is_new%22, false);%0D%0A            post = post.get(%22
+;%0D%0A        post.set(%22is_new%22, false);%0D%0A        post.get(%22parent%22).then(function(parent_post) %7B%0D%0A            if (parent_post) %7B%0D%0A                self.mark_as_read(
 parent
-%22
+_post
 );%0D%0A
@@ -871,33 +871,50 @@
 post);%0D%0A        
-%7D
+    %7D%0D%0A        %7D);
 %0D%0A    %7D,%0D%0A%0D%0A    
@@ -1014,24 +1014,24 @@
 acting%22));%0D%0A
-
             
@@ -1048,16 +1048,33 @@
 as_read(
+this.get(%22model%22)
 );%0D%0A    

Fix thread marking.