import { AddValueRequest, AddValueResponse, Logging } from "./out/index";

// Logging.verbose = false;

import * as Client from "./out/index_client";
import * as Server from "./out/index_server";
import { VerificationError } from "./out/ts_base";

const client = new Client.ServiceProvider((msg) => {
   session.onMessage(msg);
});

const server = new Server.ServiceProvider();

const session = server.getSession((msg) => {
   client.onPacket(msg);
});

class TestService extends Server.TestService<undefined> {
   async AddValuesSingleParam(
      request: AddValueRequest,
      ctx: undefined
   ): Promise<AddValueResponse> {
      return {
         value: request.value1 + request.value2,
      };
   }
   async AddValuesMultipleParams(
      value1: number,
      value2: number,
      ctx: undefined
   ): Promise<number> {
      return value1 + value2;
   }

   async ReturningVoid(param1) {
      console.log("Calling Returning Void");
   }

   OnEvent(param1: string, ctx: undefined): void {
      console.log("Received notification", param1);
   }

   async FunctionWithArrayAsParamAndReturn(vals1, vals2) {
      return [...vals1, ...vals2];
   }
}

server.addService(new TestService());

const test = new Client.TestService(client);

async function run() {
   console.log("Testing AddValuesSingleParam");
   console.log(
      await test.AddValuesSingleParam({
         value1: 1,
         value2: 2,
      })
   );

   console.log("Testing AddValuesMultipleParams");
   console.log(await test.AddValuesMultipleParams(1, 2));

   console.log("Testing Notification");
   test.OnEvent("Hi, this is an event");

   console.log("Let verification fail!");
   await test
      //@ts-ignore
      .AddValuesMultipleParams("asd", 2)
      .then(() => {
         console.log("!!!!This should have failed!!!!");
      })
      .catch((err) => {
         if (err instanceof VerificationError)
            console.log(
               "Found expected error!",
               {
                  type: err.type,
                  field: err.field,
                  value: err.value
               }
            );
         else {
            console.error(err);
            throw new Error("Unexpected Error");
         }
      });

   console.log("Test with arrays:");
   console.log(
      await test
         //@ts-ignore
         .FunctionWithArrayAsParamAndReturn([1, 2, 3], [4, 5, 6])
   );

   console.log("Let with Array fail!");
   await test
      //@ts-ignore
      .FunctionWithArrayAsParamAndReturn([1, 2, 3], [4, "asd", 6])
      .then(() => {
         console.log("!!!!This should have failed!!!!");
      })
      .catch((err) => {
         if (err instanceof VerificationError)
            console.log(
               "Found expected error!",
               {
                  type: err.type,
                  field: err.field,
                  value: err.value
               }
            );
         else {
            console.error(err);
            throw new Error("Unexpected Error");
         }
      });
}

run();
