/*!
 * Copyright (c) 2017-2018 by The Funfix Project Developers.
 * Some rights reserved.
 *
 * 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.
 */

/* @flow */

import type { Throwable } from "funfix-core"
import type { HK } from "funland"
import { Monad } from "funland"
import { Option, Try, Either } from "funfix-core"
import { Scheduler } from "./scheduler"
import { ICancelable } from "./cancelable"
import { Duration } from "./time"

// Sorry for the `any` usage in this definition, however at the time of writing
// Flow 0.53 does not support a type more specific than this. You can also blame
// the Promises/A+ spec for working with `Thenable<U> | U`.
export interface IPromiseLike<+T> {
  then(onFulfilled?: (value: T) => any, onRejected?: (error: Throwable) => any): IPromiseLike<any>;
}

declare export class Future<+A> {
  value(): Option<Try<A>>;
  onComplete(f: (a: Try<A>) => void): void;
  forEach(f: (A) => void): void;
  cancel(): void;

  withScheduler(ec: Scheduler): Future<A>;
  transformWith<B>(failure: (e: Throwable) => Future<B>, success: (a: A) => Future<B>): Future<B>;
  transform<B>(failure: (e: Throwable) => B, success: (a: A) => B): Future<B>;
  attempt(): Future<Either<Throwable, A>>;

  flatMap<B>(f: (a: A) => Future<B>): Future<B>;
  chain<B>(f: (a: A) => Future<B>): Future<B>;
  ap<B>(ff: Future<(a: A) => B>): Future<B>;
  map<B>(f: (a: A) => B): Future<B>;
  recoverWith<AA>(f: (e: Throwable) => Future<AA>): Future<A | AA>;
  recover<AA>(f: (e: Throwable) => AA): Future<A | AA>;
  toPromise(): Promise<A>;

  delayResult(delay: number | Duration): Future<A>;
  timeoutTo<AA>(after: number | Duration, fallback: () => Future<AA>): Future<A | AA>;
  timeout(after: number | Duration): Future<A>;

  // From IPromiseLike
  then(onFulfilled?: (value: A) => any, onRejected?: (error: Throwable) => any): Future<any>;

  static +_Class: Future<any>;

  static of<A>(thunk: () => A, ec?: Scheduler): Future<A>;
  static pure<A>(a: A, ec?: Scheduler): Future<A>;
  static raise<A>(e: Throwable, ec?: Scheduler): Future<A>;
  static create<A>(register: (cb: (a: Try<A>) => void) => (ICancelable | void), ec?: Scheduler): Future<A>;
  static unit(): Future<void>;
  static delayedTick<A>(delay: number | Duration, ec?: Scheduler): Future<void>;
  static tailRecM<A, B>(a: A, f: (a: A) => Future<Either<A, B>>, ec?: Scheduler): Future<B>;
  static fromPromise<A>(ref: IPromiseLike<A>, ec?: Scheduler): Future<A>;
  static fromTry<A>(value: Try<A>, ec?: Scheduler): Future<A>;

  static firstCompletedOf<A>(list: Future<A>[] | Iterable<Future<A>>, ec?: Scheduler): Future<A>;
  static sequence<A>(list: Future<A>[] | Iterable<Future<A>>, ec?: Scheduler): Future<A[]>;
  static traverse<A>(list: A[] | Iterable<A>, parallelism?: number, ec?: Scheduler):
    <B>(f: (a: A) => Future<B>) => Future<B[]>;

  static map2<A1, A2, R>(
    fa1: Future<A1>, fa2: Future<A2>, f: (a1: A1, a2: A2) => R,
    ec?: Scheduler): Future<R>;
  static map3<A1, A2, A3, R>(
    fa1: Future<A1>, fa2: Future<A2>, fa3: Future<A3>,
    f: (a1: A1, a2: A2, a3: A3) => R,
    ec?: Scheduler): Future<R>;
  static map4<A1, A2, A3, A4, R>(
    fa1: Future<A1>, fa2: Future<A2>, fa3: Future<A3>, fa4: Future<A4>,
    f: (a1: A1, a2: A2, a3: A3, a4: A4) => R,
    ec?: Scheduler): Future<R>;
  static map5<A1, A2, A3, A4, A5, R>(
    fa1: Future<A1>, fa2: Future<A2>, fa3: Future<A3>, fa4: Future<A4>, fa5: Future<A5>,
    f: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => R,
    ec?: Scheduler): Future<R>;
  static map6<A1, A2, A3, A4, A5, A6, R>(
    fa1: Future<A1>, fa2: Future<A2>, fa3: Future<A3>, fa4: Future<A4>, fa5: Future<A5>, fa6: Future<A6>,
    f: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) => R,
    ec?: Scheduler): Future<R>;
}

export type FutureURI = <U, L, A>(x: [U, L, A]) => Future<A>;
export type FutureK<A> = HK<FutureURI, A>;

export type FutureTypes = Monad<FutureURI>;
declare export var FutureModule: FutureTypes;

declare export class FutureMaker<A> {
  +tryComplete: (result: Try<A>) => boolean;
  +complete: (result: Try<A>) => void;

  trySuccess(value: A): boolean;
  success(value: A): void;
  tryFailure(error: Throwable): boolean;
  failure(error: Throwable): void;

  chainTo(target: FutureMaker<A>): void;
  future(cancelable?: ICancelable): Future<A>;
  withScheduler(ec: Scheduler): FutureMaker<A>;

  static empty<A>(ec?: Scheduler): FutureMaker<A>;
  static completed<A>(value: Try<A>, ec?: Scheduler): FutureMaker<A>;
}
