/*
    This file is part of TON Blockchain Library.
    TON Blockchain Library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.
    TON Blockchain Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License
    along with TON Blockchain Library.  If not, see .
    Copyright 2017-2019 Telegram Systems LLP
*/
#pragma once
#include "td/actor/actor.h"
#include "td/utils/buffer.h"
#include "td/utils/Status.h"
#include "td/actor/PromiseFuture.h"
#include "auto/tl/ton_api.h"
namespace ton {
class Encryptor {
 public:
  virtual td::Result encrypt(td::Slice data) = 0;
  virtual td::Status check_signature(td::Slice message, td::Slice signature) = 0;
  virtual ~Encryptor() = default;
  static td::Result> create(const ton_api::PublicKey *id);
};
class Decryptor {
 public:
  virtual td::Result decrypt(td::Slice data) = 0;
  virtual td::Result sign(td::Slice data) = 0;
  virtual std::vector> sign_batch(std::vector data);
  virtual ~Decryptor() = default;
  static td::Result> create(const ton_api::PrivateKey *id);
};
class EncryptorAsync : public td::actor::Actor {
 private:
  std::unique_ptr encryptor_;
 public:
  EncryptorAsync(std::unique_ptr encryptor) : encryptor_(std::move(encryptor)) {
  }
  void check_signature(td::BufferSlice data, td::BufferSlice signature, td::Promise promise) {
    auto res = encryptor_->check_signature(data.as_slice(), signature.as_slice());
    if (res.is_ok()) {
      promise.set_value(td::Unit());
    } else {
      promise.set_error(res.move_as_error());
    }
  }
  void encrypt(td::BufferSlice data, td::Promise promise) {
    promise.set_result(encryptor_->encrypt(data.as_slice()));
  }
  template 
  static td::Result> create(T &id) {
    TRY_RESULT(d, Encryptor::create(id));
    return td::actor::create_actor("encryptor", std::move(d));
  }
  template 
  static td::Result> create(T *id) {
    TRY_RESULT(d, Encryptor::create(id));
    return td::actor::create_actor("encryptor", std::move(d));
  }
};
class DecryptorAsync : public td::actor::Actor {
 private:
  std::unique_ptr decryptor_;
 public:
  DecryptorAsync(std::unique_ptr decryptor) : decryptor_(std::move(decryptor)) {
  }
  auto decrypt(td::BufferSlice data) {
    return decryptor_->decrypt(data.as_slice());
  }
  auto sign(td::BufferSlice data) {
    return decryptor_->sign(data.as_slice());
  }
  auto sign_batch(std::vector data) {
    std::vector v;
    v.resize(data.size());
    for (size_t i = 0; i < data.size(); i++) {
      v[i] = data[i].as_slice();
    }
    return decryptor_->sign_batch(v);
  }
  template 
  static td::Result> create(T &id) {
    TRY_RESULT(d, Decryptor::create(id));
    return td::actor::create_actor("decryptor", std::move(d));
  }
  template 
  static td::Result> create(T *id) {
    TRY_RESULT(d, Decryptor::create(id));
    return td::actor::create_actor("decryptor", std::move(d));
  }
};
}  // namespace ton