import { Schema as LogSchemaOptions } from './schema';
import {
  Rule,
  Tree,
  SchematicContext,
  chain,
  mergeWith,
  apply,
  url,
  move,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import {addPackageToPackageJson} from "../utils/json";
import {getProject, Project} from "../utils/project";
import {insertContent, MODULE_ROUTE_CONTENTS, routeModules, routingModuleContent} from "../utils/content";

let project: Project;

function addDependenciesToPackageJson(options: LogSchemaOptions) {
  return (host: Tree, context: SchematicContext) => {
    addPackageToPackageJson(host,
      [
        `@ecip/log@1.0.0-rc.1`,
      ],'dependencies')
  };
}

function installPackages(): Rule {
  return (host: Tree, context: SchematicContext) => {
    context.addTask(new NodePackageInstallTask());
    return host;
  };
}

function overwriteRoutingModule(options: LogSchemaOptions) {
  return (tree: Tree, context: SchematicContext) => {
    // 读取文件内容
    console.log(options);
    const routingModule = tree.get(`${project.sourceRoot}/app/routes/routes-routing.module.ts`)
    if (routingModule) {
      return;
    }
    const content = routingModule.content.toString().replace('// business替换，请勿删除',
      `${options.business}\n\t\t\t// business替换，请勿删除`);
    tree.delete(`${project.sourceRoot}/app/routes/routes-routing.module.ts`);

    tree.create(`${project.sourceRoot}/app/routes/routes-routing.module.ts`, content);
  };
}

// function overwriteRoutingModule() {
//   return (tree: Tree, context: SchematicContext) => {
//     const routesStatus = routeModules(tree, project.sourceRoot);
//     const routingContent = tree.get(`${project.sourceRoot}/app/routes/routes-routing.module.ts`).content.toString();
//     // route-routing.module.ts 里面在loadchildren没有对应的功能模块子路由
//     if (routesStatus[routesStatus.length-1] === -1) {
//       console.log(routesStatus.length-1);
//       tree.overwrite(`${project.sourceRoot}/app/routes/routes-routing.module.ts`,
//         routingModuleContent([MODULE_ROUTE_CONTENTS.LOG]));
//     } else {
//       if (routingContent.indexOf(MODULE_ROUTE_CONTENTS.LOG.trim()) === -1) {
//         tree.overwrite(`${project.sourceRoot}/app/routes/routes-routing.module.ts`,
//           insertContent(routingContent, routesStatus[routesStatus.lastIndexOf(-1) + 1], MODULE_ROUTE_CONTENTS.LOG + '\n\t\t\t'));
//       }
//     }
//   };
// }

function addFilesToRoot(options: LogSchemaOptions) {
  return chain([
    mergeWith(
      apply(url('./files'), [
        move(`${project.sourceRoot}/app/routes`),
      ]),
    ),
  ]);
}

export default function(options: LogSchemaOptions): Rule {
  return (host: Tree, context: SchematicContext) => {
    project = getProject(host, options.project);
    return chain([
      overwriteRoutingModule(options),
      addDependenciesToPackageJson(options),
      addFilesToRoot(options),
      installPackages(),
    ])(host, context);
  };
}
