'use strict';

const WunderlistSdk = require('Wunderlist');
const Configuration = require('./util/configuration');

module.exports = class CreateCommand {

  constructor() {
    this.command = "create";

    const configuration = new Configuration();
    configuration.load();
    this.wunderListClient = new WunderlistSdk({
      accessToken: configuration.accessToken,
      clientID: configuration.clientId
    });
  }

  execute() {
    // FIX ME:use Promise like below.
    // const wlbatch = new WlBatch({at, clientID});
    // wlbatch.listUpLists().acceptUserInput().addTask();

    this.wunderListClient.http.lists.all()
      .done(lists => {
        console.log("Select one of indexes below to add a recurrence task");
        lists.forEach((list, index) => {
          console.log("%d: %s(%d)", index, list.title, list.id);
        });

        console.log("> ");

        process.stdin.resume();
        process.stdin.setEncoding('utf8');

        var selectedListId = '';
        process.stdin.on('data', chunk => {
          selectedListId += chunk;
        });

        process.stdin.on('end', () => {
          // There will be a trailing \n from the user hitting enter. Get rid of it.
          selectedListId = selectedListId.replace(/\n$/, '');
          console.log("selectedListId: %s", selectedListId);

          const obj = {
            list_id: parseInt(selectedListId, 10),           // eslint-disable-line camelcase
            title: "WLBatch test"
          };
          console.log(obj);

          this.wunderListClient.http.tasks.create(obj)
            .done(res => {
              console.log(res);
            })
            .fail(error => {
              console.error('Failed to create a task: %s', JSON.stringify(error));
            });
        });
      })
      .fail(error => {
        console.error('Failed to get lists: %s', JSON.stringify(error));
      });
  }

};

@@ -266,24 +266,25 @@
 ion.load();%0A
+%0A
     this.wun
@@ -449,286 +449,197 @@
  ME:
-use Promise like below.%0A    // const wlbatch = new WlBatch(%7Bat, clientID%7D);%0A    // wlbatch.listUpLists().acceptUserInput().addTask();%0A%0A    this.wunderListClient.http.lists.all()%0A      .done(lists =%3E %7B%0A        console.log(%22Select one of indexes below to add a recurrence task
+ refactor with Promise.%0A    this.wunderListClient.http.lists.all()%0A      .done(lists =%3E %7B%0A        var indexHash = %7B%7D;%0A%0A        console.log(%22You have the followings lists in Wunderlist.
 %22);%0A
+%0A
     
@@ -752,32 +752,176 @@
     
-%7D);%0A%0A        console.log
+  indexHash%5Bindex%5D = list.id;%0A        %7D);%0A%0A        console.log(%22%22);%0A        console.log(%22Select one of indexes to add a recurrence task%22);%0A%0A        process.stdout.write
 (%22%3E 
@@ -1021,22 +1021,21 @@
 selected
-ListId
+Index
  = '';%0A 
@@ -1095,22 +1095,21 @@
 selected
-ListId
+Index
  += chun
@@ -1160,24 +1160,67 @@
 d', () =%3E %7B%0A
+          // FIX ME: handle out of index.%0A%0A
           //
@@ -1310,22 +1310,21 @@
 selected
-ListId
+Index
  = selec
@@ -1326,22 +1326,21 @@
 selected
-ListId
+Index
 .replace
@@ -1352,16 +1352,75 @@
 /, '');%0A
+          console.log(%22selectedIndex: %25s%22, selectedIndex);%0A
         
@@ -1455,24 +1455,34 @@
 d: %25s%22, 
+indexHash%5B
 selected
 ListId);
@@ -1473,22 +1473,22 @@
 selected
-ListId
+Index%5D
 );%0A%0A    
@@ -1537,24 +1537,34 @@
 arseInt(
+indexHash%5B
 selected
 ListId, 
@@ -1555,22 +1555,22 @@
 selected
-ListId
+Index%5D
 , 10),  

Support index input on create.