// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {Actions} from '../common/actions';
import * as trace_to_text from '../gen/trace_to_text';

import {globals} from './globals';

export function ConvertTrace(trace: Blob, truncate?: 'start'|'end') {
  const mod = trace_to_text({
    noInitialRun: true,
    locateFile: (s: string) => s,
    print: updateStatus,
    printErr: updateStatus,
    onRuntimeInitialized: () => {
      updateStatus('Converting trace');
      const outPath = '/trace.json';
      if (truncate === undefined) {
        mod.callMain(['json', '/fs/trace.proto', outPath]);
      } else {
        mod.callMain(
            ['json', '--truncate', truncate, '/fs/trace.proto', outPath]);
      }
      updateStatus('Trace conversion completed');
      const fsNode = mod.FS.lookupPath(outPath).node;
      const data = fsNode.contents.buffer;
      const size = fsNode.usedBytes;
      globals.publish('LegacyTrace', {data, size}, /*transfer=*/[data]);
      mod.FS.unlink(outPath);
    },
    onAbort: () => {
      console.log('ABORT');
    },
  });
  mod.FS.mkdir('/fs');
  mod.FS.mount(
      mod.FS.filesystems.WORKERFS,
      {blobs: [{name: 'trace.proto', data: trace}]},
      '/fs');

  // TODO removeme.
  (self as {} as {mod: {}}).mod = mod;
}

export async function ConvertTraceToPprof(
    pid: number, src: string|File|ArrayBuffer, ts1: number, ts2?: number) {
  generateBlob(src).then(result => {
    const mod = trace_to_text({
      noInitialRun: true,
      locateFile: (s: string) => s,
      print: updateStatus,
      printErr: updateStatus,
      onRuntimeInitialized: () => {
        updateStatus('Converting trace');
        const timestamps = `${ts1}${ts2 === undefined ? '' : `,${ts2}`}`;
        mod.callMain([
          'profile',
          `--pid`,
          `${pid}`,
          `--timestamps`,
          timestamps,
          '/fs/trace.proto'
        ]);
        updateStatus('Trace conversion completed');
        const heapDirName =
            Object.keys(mod.FS.lookupPath('/tmp/').node.contents)[0];
        const heapDirContents =
            mod.FS.lookupPath(`/tmp/${heapDirName}`).node.contents;
        const heapDumpFiles = Object.keys(heapDirContents);
        let fileNum = 0;
        heapDumpFiles.forEach(heapDump => {
          const fileContents =
              mod.FS.lookupPath(`/tmp/${heapDirName}/${heapDump}`)
                  .node.contents;
          fileNum++;
          const fileName = `/heap_dump.${fileNum}.${pid}.pb`;
          downloadFile(new Blob([fileContents]), fileName);
        });
        updateStatus('Profile(s) downloaded');
      },
      onAbort: () => {
        console.log('ABORT');
      },
    });
    mod.FS.mkdir('/fs');
    mod.FS.mount(
        mod.FS.filesystems.WORKERFS,
        {blobs: [{name: 'trace.proto', data: result}]},
        '/fs');
  });
}

async function generateBlob(src: string|ArrayBuffer|File) {
  let blob: Blob = new Blob();
  if (typeof src === 'string') {
    const resp = await fetch(src);
    if (resp.status !== 200) {
      throw new Error(`fetch() failed with HTTP error ${resp.status}`);
    }
    blob = await resp.blob();
  } else if (src instanceof ArrayBuffer) {
    blob = new Blob([new Uint8Array(src, 0, src.byteLength)]);
  } else {
    blob = src;
  }
  return blob;
}

function downloadFile(file: Blob, name: string) {
  globals.publish('FileDownload', {file, name});
}

function updateStatus(msg: {}) {
  console.log(msg);
  globals.dispatch(Actions.updateStatus({
    msg: msg.toString(),
    timestamp: Date.now() / 1000,
  }));
}
