/// <reference path="../lib/yeoman-generator.d.ts" />
/* global __dirname */

'use strict';
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');
var path = require('path');
// same for library and executable
var solutionGUID = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"


module.exports = yeoman.generators.Base.extend({

  init: function () {
    this.log(yosay('Welcome to the .NET Core v5 generator!'));
  },

  askFor: function () {
    var done = this.async();

    var prompts = [{
      type: 'list',
      name: 'type',
      message: 'What type of application do you want to create?',
      choices: [
        {
          name: 'Executable',
          value: 'executable'
        },
        {
          name: 'Library',
          value: 'library'
        }
      ]
    }];

    this.prompt(prompts, function (props) {
      this.type = props.type;

      done();
    }.bind(this));
  },

  askForName: function () {
    var done = this.async();
    var app = '';
    switch (this.type) {
      case 'executable':
        app = 'MyExecutable';
        break;
      case 'library':
        app = 'MyLibrary'
        break;
    }
    var prompts = [{
      name: 'applicationName',
      message: 'What\'s the name of your .NET application?',
      default: app
    }];
    this.prompt(prompts, function (props) {
      this.applicationName = props.applicationName;
      this.namespace = props.applicationName;
      done();
    }.bind(this));
  },

  askForClassName: function () {
    var done = this.async();
    var cname = 'Class1';
    
    if (this.type === 'library') {  
      var prompts = [{
        name: 'className',
        message: 'Class Name?',
        default: cname
      }];
      this.prompt(prompts, function (props) {
        this.className = props.className;
        done();
      }.bind(this));
    }
  },
  
  writing: {
    app: function () {

      this.projectGUID = guid();
      this.assemblyGUID = guid();
      
      this.mkdir(this.applicationName);
      this.sourceRoot(path.join(__dirname, '../templates/projects/' + this.type));
      console.log('sourceRoot: ' + this.sourceRoot());

      switch (this.type) {
        case 'executable':
          this.sourceRoot(path.join(__dirname, '../templates/projects/' + this.type));
          console.log(this.applicationName + '/Program.cs');

          this.template(this.sourceRoot() + '/Program.cs', this.applicationName + '/Program.cs', {
            applicationName: this.applicationName
          });

          this.copy(this.sourceRoot() + '/App.config', this.applicationName + '/App.config');

          this.template(this.sourceRoot() + '/Executable.sln', this.applicationName + '/' + this.applicationName + '.sln', {
            solutionGUID: solutionGUID,
            applicationName: this.applicationName,
            projectGUID: this.projectGUID
          });

          this.template(this.sourceRoot() + '/Executable.csproj', this.applicationName + '/' + this.applicationName + '.sln', {
            projectGUID: this.projectGUID,
            namespace: this.applicationName,
            assemblyName: this.assemblyName
          });

          this.template(this.sourceRoot() + '/Properties/AssemblyInfo.cs', this.applicationName + '/Properties/AssemblyInfo.cs', {
            assemblyGUID: this.assemblyGuid,
            applicationName: this.applicationName
          });
          
          break;
        case 'library':
          this.sourceRoot(path.join(__dirname, '../templates/projects/' + this.type));
          console.log(this.applicationName + '/Program.cs');

          this.template(this.sourceRoot() + '/Class1.cs', this.applicationName + '/Class1.cs', {
            namespace: this.namespace,
            className: this.className
          });

//          this.copy(this.sourceRoot() + '/App.config', this.applicationName + '/App.config');

          this.template(this.sourceRoot() + '/MyLibrary.sln', this.applicationName + '/' + this.applicationName + '.sln', {
            solutionGUID: solutionGUID,
            applicationName: this.applicationName,
            projectGUID: this.projectGUID
          });

          this.template(this.sourceRoot() + '/MyLibrary.csproj', this.applicationName + '/' + this.applicationName + '.sln', {
            projectGUID: this.projectGUID,
            namespace: this.applicationName,
            assemblyName: this.assemblyName
          });

          this.template(this.sourceRoot() + '/Properties/AssemblyInfo.cs', this.applicationName + '/Properties/AssemblyInfo.cs', {
            assemblyGUID: this.assemblyGuid,
            applicationName: this.applicationName
          });
          
          break;

        default:
          console.log("Unknown App Type");
      }
    }
  },  
  
  //  writing: {
  //    app: function () {
  //      this.fs.copy(
  //        this.templatePath('_package.json'),
  //        this.destinationPath('package.json')
  //      );
  //      this.fs.copy(
  //        this.templatePath('_bower.json'),
  //        this.destinationPath('bower.json')
  //      );
  //    },
  //
  //    projectfiles: function () {
  //      this.fs.copy(
  //        this.templatePath('editorconfig'),
  //        this.destinationPath('.editorconfig')
  //      );
  //      this.fs.copy(
  //        this.templatePath('jshintrc'),
  //        this.destinationPath('.jshintrc')
  //      );
  //    }
  //  },
  //
  //  install: function () {
  //    this.installDependencies({
  //      skipInstall: this.options['skip-install']
  //    });
  //  },
  
  end: function () {
    // do nothing
  }



});

function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
  
}
