import { UnsignedEvent, EventTemplate } from 'nostr-tools'
import { SignedEvent } from '../../types'
import {
  LoginMethodStrategy,
  LoginMethodOperations
} from './loginMethodStrategy'
import { LoginMethod } from '../../store/auth/types'
import { NostrLoginStrategy } from './NostrLoginStrategy'
import { PrivateKeyStrategy } from './PrivateKeyStrategy'

/**
 * This class is a context provider and helper class. This MUST be instantiated and used as an entry point for any of the {@link LoginMethodOperations LoginMethodOperations}
 * @constructor Takes {@link LoginMethod LoginMethod} as an argument and sets the correct strategy
 *
 * @see {@link LoginMethod}
 * @see {@link LoginMethodOperations}
 */
export class LoginMethodContext implements LoginMethodOperations {
  private strategy: LoginMethodStrategy

  constructor(loginMethod?: LoginMethod) {
    switch (loginMethod) {
      case LoginMethod.nostrLogin:
        this.strategy = new NostrLoginStrategy()
        break
      case LoginMethod.privateKey:
        this.strategy = new PrivateKeyStrategy()
        break
      default:
        this.strategy = new LoginMethodStrategy()
        break
    }
  }

  nip04Encrypt(receiver: string, content: string): Promise<string> {
    return this.strategy.nip04Encrypt(receiver, content)
  }
  nip04Decrypt(sender: string, content: string): Promise<string> {
    return this.strategy.nip04Decrypt(sender, content)
  }
  nip44Encrypt(receiver: string, content: string): Promise<string> {
    return this.strategy.nip44Encrypt(receiver, content)
  }
  nip44Decrypt(sender: string, content: string): Promise<string> {
    return this.strategy.nip44Decrypt(sender, content)
  }
  signEvent(event: UnsignedEvent | EventTemplate): Promise<SignedEvent> {
    return this.strategy.signEvent(event)
  }
}
