'use babel';

import Connector from './../connectors/connector.js';
import { $ } from 'atom-space-pen-views';
import { View } from 'atom-space-pen-views';

var FileSystem = require('fs');
const tempDirectory = require('os')
  .tmpdir();

class FileView extends View {

  FileView() {
    super.FileView();

    const self = this;

    self.parent = null;
    self.config = null;
    self.name = '';
  }

  static content() {
    return this.li({
      class: 'file entry list-item',
    }, () => this.span({
      class: 'name icon',
      outlet: 'label',
    }));
  };

  initialize(parent, file) {
    const self = this;

    self.parent = parent;
    self.config = parent.config;
    self.name = file.name;

    self.label.text(self.name);
    self.label.addClass('icon-file-text');

    self.attr('data-name', self.name);
    self.attr('data-host', self.config.host);

    // Events
    self.on('click', function (e) {
      e.stopPropagation();
      self.open();
    });

    self.on('dblclick', function (e) {
      e.stopPropagation();
    });
  };

  destroy() {
    this.remove();
  };

  getRoot() {
    const self = this;

    if (self.parent) {
      return self.parent.getRoot();
    }
    return self;
  }

  getPath() {
    const self = this;

    return self.parent.getPath();
  }

  getLocalPath() {
    const self = this;

    return self.parent.getLocalPath();
  }

  open() {
    const self = this;

    self.label.addClass('icon-sync')
      .addClass('spin')
      .removeClass('icon-file-text');

    self.getRoot().connector.existsFile(self.getPath() + self.name)
      .then((file) => {
        if (file !== undefined && file !== null) {

          // Create local Directories
          self.checkPath(self.getLocalPath() + self.name);

          // Download file and open
          self.getRoot().connector.downloadFile(self.getPath() + self.name, self.getLocalPath() + self.name)
            .then(() => {
              self.label.removeClass('icon-sync')
                .removeClass('spin')
                .addClass('icon-file-text');
              return atom.workspace.open(self.getLocalPath() + self.name);
            })
            .catch(function (err) {
              self.label.removeClass('icon-sync')
                .removeClass('spin')
                .addClass('icon-file-text');
            })
            .then((editor) => {
              if (self.editor === null || self.editor === undefined) {
                self.editor = editor;

                // Save file on remote server
                editor.onDidSave((saveObject) => {
                  self.getRoot().connector.uploadFile(editor.getText(), self.getPath() + self.name, editor.getPath())
                    .then(() => {
                      self.getRoot().connector.showMessage('File successfully uploaded.', 'success');
                    })
                    .catch(function (err) {
                      self.getRoot().connector.showMessage(err.message, 'error');
                    });
                });

                editor.onDidDestroy(() => {
                  self.editor = null;
                });
              }
            });
        }
      })
      .catch(function (err) {
        self.getRoot().connector.showMessage(err.message, 'error');
        self.label.removeClass('icon-sync')
          .removeClass('spin')
          .addClass('icon-file-text');
      });
  }

  rename(relativePath) {
    const self = this;
    let fullRelativePath = ('/' + self.config.remote + '/' + relativePath).replace(/\/+/g, "/");

    self.getRoot().connector.rename(self.getPath(true) + self.name, fullRelativePath.trim())
      .then(() => {
        let pathArr = relativePath.trim()
          .split('/');

        self.name = pathArr.pop();
        self.label.text(self.name);
        self.attr('data-name', self.name);
      })
      .catch(function (err) {
        self.getRoot().connector.showMessage(err.message, 'error');
      });
  }

  delete() {
    const self = this;

    self.getRoot().connector.deleteFile(self.getPath(true) + self.name)
      .then(() => {
        self.destroy();
      })
      .catch(function (err) {
        self.getRoot().connector.showMessage(err.message, 'error');
      });
  }

  checkPath(localpath) {
    let arrPath = localpath.split('/');
    arrPath.pop();

    arrPath.reduce((path, dir) => {
      path += '/' + dir;
      if (!FileSystem.existsSync(path)) {
        FileSystem.mkdirSync(path);
      }
      return path;
    });

  }
}

module.exports = FileView;

@@ -1222,24 +1222,40 @@
 %0A%0A  getPath(
+useRemote = true
 ) %7B%0A    cons
@@ -1297,24 +1297,33 @@
 ent.getPath(
+useRemote
 );%0A  %7D%0A%0A  ge
@@ -1333,16 +1333,32 @@
 calPath(
+useRemote = true
 ) %7B%0A    
@@ -1413,16 +1413,25 @@
 calPath(
+useRemote
 );%0A  %7D%0A%0A
@@ -1610,32 +1610,36 @@
 le(self.getPath(
+true
 ) + self.name)%0A 
@@ -1786,32 +1786,36 @@
 lf.getLocalPath(
+true
 ) + self.name);%0A
@@ -1904,32 +1904,36 @@
 le(self.getPath(
+true
 ) + self.name, s
@@ -1941,32 +1941,36 @@
 lf.getLocalPath(
+true
 ) + self.name)%0A 
@@ -2181,24 +2181,28 @@
 etLocalPath(
+true
 ) + self.nam
@@ -2727,16 +2727,20 @@
 getPath(
+true
 ) + self
@@ -2761,16 +2761,20 @@
 getPath(
+true
 ))%0A     

Fix error in rename dialog. Intitial directory was added twice, first in the path and later in the (s)ftp command.