import { Injectable } from '@angular/core'
import { pauseFor } from '@start9labs/shared'
import { ApiService, CifsRecoverySource, SetupEmbassyReq } from './api.service'

let tries = 0

@Injectable({
  providedIn: 'root',
})
export class MockApiService extends ApiService {
  constructor() {
    super()
  }

  // ** UNENCRYPTED **

  async getStatus() {
    await pauseFor(1000)
    return {
      'product-key': true,
      migrating: false,
    }
  }

  async getDrives() {
    await pauseFor(1000)
    return {
      disks: [
        {
          logicalname: 'abcd',
          vendor: 'Samsung',
          model: 'T5',
          partitions: [
            {
              logicalname: 'pabcd',
              label: null,
              capacity: 73264762332,
              used: null,
              'embassy-os': {
                version: '0.2.17',
                full: true,
                'password-hash': null,
                'wrapped-key': null,
              },
            },
          ],
          capacity: 123456789123,
          guid: 'uuid-uuid-uuid-uuid',
        },
      ],
      reconnect: [],
    }
  }

  async set02XDrive() {
    await pauseFor(1000)
    return
  }

  async getRecoveryStatus() {
    tries = Math.min(tries + 1, 4)
    return {
      'bytes-transferred': tries,
      'total-bytes': 4,
      complete: tries === 4,
    }
  }

  // ** ENCRYPTED **

  async verifyCifs(params: CifsRecoverySource) {
    await pauseFor(1000)
    return {
      version: '0.3.0',
      full: true,
      'password-hash':
        '$argon2d$v=19$m=1024,t=1,p=1$YXNkZmFzZGZhc2RmYXNkZg$Ceev1I901G6UwU+hY0sHrFZ56D+o+LNJ',
      'wrapped-key': '',
    }
  }

  async verifyProductKey() {
    await pauseFor(1000)
    return
  }

  async importDrive(guid: string) {
    await pauseFor(3000)
    return setupRes
  }

  async setupEmbassy(setupInfo: SetupEmbassyReq) {
    await pauseFor(3000)
    return setupRes
  }

  async setupComplete() {
    await pauseFor(1000)
    return setupRes
  }
}

const rootCA = `-----BEGIN CERTIFICATE-----
MIIDpzCCAo+gAwIBAgIRAIIuOarlQETlUQEOZJGZYdIwDQYJKoZIhvcNAQELBQAw
bTELMAkGA1UEBhMCVVMxFTATBgNVBAoMDEV4YW1wbGUgQ29ycDEOMAwGA1UECwwF
U2FsZXMxCzAJBgN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LNIuCuNXJOwU17Rdl1W
YJidaq7je6k18AdgPA0Kh8y1XtfUH3fTaVw4
-----END CERTIFICATE-----`

const setupRes = {
  'tor-address': 'http://asdafsadasdasasdasdfasdfasdf.onion',
  'lan-address': 'https://embassy-abcdefgh.local',
  'root-ca': btoa(rootCA),
}

const disks = [
  {
    vendor: 'Samsung',
    model: 'SATA',
    logicalname: '/dev/sda',
    guid: 'theguid',
    partitions: [
      {
        logicalname: 'sda1',
        label: 'label 1',
        capacity: 100000,
        used: 200.1255312,
        'embassy-os': null,
      },
      {
        logicalname: 'sda2',
        label: 'label 2',
        capacity: 50000,
        used: 200.1255312,
        'embassy-os': null,
      },
    ],
    capacity: 150000,
  },
  {
    vendor: 'Samsung',
    model: null,
    logicalname: 'dev/sdb',
    partitions: [],
    capacity: 34359738369,
    guid: null,
  },
  {
    vendor: 'Crucial',
    model: 'MX500',
    logicalname: 'dev/sdc',
    guid: null,
    partitions: [
      {
        logicalname: 'sdc1',
        label: 'label 1',
        capacity: 0,
        used: null,
        'embassy-os': {
          version: '0.3.3',
          full: true,
          'password-hash': 'asdfasdfasdf',
          'wrapped-key': '',
        },
      },
      {
        logicalname: 'sdc1MOCKTESTER',
        label: 'label 1',
        capacity: 0,
        used: null,
        'embassy-os': {
          version: '0.3.6',
          full: true,
          // password is 'asdfasdf'
          'password-hash':
            '$argon2d$v=19$m=1024,t=1,p=1$YXNkZmFzZGZhc2RmYXNkZg$Ceev1I901G6UwU+hY0sHrFZ56D+o+LNJ',
          'wrapped-key': '',
        },
      },
      {
        logicalname: 'sdc1',
        label: 'label 1',
        capacity: 0,
        used: null,
        'embassy-os': {
          version: '0.3.3',
          full: false,
          'password-hash': 'asdfasdfasdf',
          'wrapped-key': '',
        },
      },
    ],
    capacity: 100000,
  },
  {
    vendor: 'Sandisk',
    model: null,
    logicalname: '/dev/sdd',
    guid: null,
    partitions: [
      {
        logicalname: 'sdd1',
        label: null,
        capacity: 10000,
        used: null,
        'embassy-os': {
          version: '0.2.7',
          full: true,
          'password-hash': 'asdfasdfasdf',
          'wrapped-key': '',
        },
      },
    ],
    capacity: 10000,
  },
]
