import ActionDispatcher from '../dispatcher/action-dispatcher'
import EditorConstants from '../constants/editor-constants'
import TwitterClientConstants from '../constants/twitter-client-constants'
import StoreBase from './store-base'
import TweetOptionConstants from '../constants/tweet-option-constants'
import TwitterText from 'twitter-text'

class Media {
    constructor(media_id, path) {
        this.media_id = media_id;
        this.path = path;
    }
}

export default class EditorStore extends StoreBase {
    constructor() {
        super();

        this._isActiveEditor = false;
        this._isControllable = false;
        this._editorText = "";
        this._mediaList = [];

        ActionDispatcher.register((action) => {
            switch(action.actionType) {
                case EditorConstants.OPEN_EDITOR:
                    this._isActiveEditor = true;
                    this._isControllable = true;
                    this.emitChange();
                    break;

                case EditorConstants.CLOSE_EDITOR:
                    this._isActiveEditor = false;
                    this._isControllable = false;
                    this.emitChange();
                    break;

                case EditorConstants.POST_TWEET:
                    this._isControllable = false;
                    this.emitChange();
                    break;

                case EditorConstants.CHANGE_TEXTAREA:
                    this._editorText = action.text;
                    this.emitChange();
                    break;

                case TwitterClientConstants.POST_COMPLETED:
                    this._editorText = "";
                    this._isControllable = true;
                    this._mediaList = [];
                    this.emitChange();
                    break;

                case TweetOptionConstants.OPEN_MENTION:
                    this._editorText = "@" + action.screenName + " ";
                    this._isActiveEditor = true;
                    this._isControllable = true;
                    this.emitChange();
                    break;

                case TweetOptionConstants.OPEN_DM:
                    this._editorText = "D @" + action.screenName + " ";
                    this._isActiveEditor = true;
                    this._isControllable = true;
                    this.emitChange();
                    break;

                case TwitterClientConstants.MEDIA_UPLOADED:
                    this._mediaList.push(new Media(action.media_id, action.path));

                default:
                    break;
            }
        });
    }

    isActiveEditor() {
        return this._isActiveEditor;
    }

    isControllable() {
        return this._isControllable;
    }

    editorText() {
        return this._editorText;
    }

    mediaList() {
        return this._mediaList;
    }

    remainTextCount() {
        return 140 - TwitterText.getTweetLength(this._editorText);
    }
}

@@ -1862,16 +1862,85 @@
 ENTION:%0A
+                    if (action.screenName)%0A                    %7B%0A    
         
@@ -2013,32 +2013,36 @@
                 
+   
+ 
 this._isActiveEd
@@ -2046,32 +2046,36 @@
 eEditor = true;%0A
+    
                 
@@ -2119,32 +2119,36 @@
                 
+ 
+   
 this.emitChange(
@@ -2142,32 +2142,54 @@
 s.emitChange();%0A
+                    %7D%0A
                 
@@ -2251,16 +2251,85 @@
 PEN_DM:%0A
+                    if (action.screenName)%0A                    %7B%0A    
         
@@ -2404,32 +2404,36 @@
                 
+   
+ 
 this._isActiveEd
@@ -2437,32 +2437,36 @@
 eEditor = true;%0A
+    
                 
@@ -2510,32 +2510,36 @@
                 
+ 
+   
 this.emitChange(
@@ -2533,32 +2533,54 @@
 s.emitChange();%0A
+                    %7D%0A
                 

fix unknown bug: open editor with undefined screen name.