package com.pr0gramm.app.services;

import android.util.Patterns;

import com.pr0gramm.app.api.pr0gramm.Api;
import com.pr0gramm.app.api.pr0gramm.response.AccountInfo;
import com.pr0gramm.app.api.pr0gramm.response.ImmutableAccountInfo;

import org.immutables.value.Value;
import org.joda.time.Instant;

import java.util.List;
import java.util.regex.Matcher;

import javax.inject.Inject;
import javax.inject.Singleton;

import rx.Observable;

/**
 */
@Singleton
public class InviteService {
    private final Api api;

    @Inject
    public InviteService(Api api) {
        this.api = api;
    }

    public Observable<Void> send(String email) {
        Matcher matcher = Patterns.EMAIL_ADDRESS.matcher(email);
        if (!matcher.matches())
            return Observable.error(new InviteException("email"));

        return api.invite(null, email).flatMap(response -> {
            if (response.error() != null) {
                return Observable.error(new InviteException(response.error()));
            } else {
                return Observable.just(null);
            }
        });
    }

    public Observable<Invites> invites() {
        return api.accountInfo().map(info -> ImmutableInvites.builder()
                .inviteCount(info.account().invites())
                .invited(info.invited())
                .addInvited(ImmutableAccountInfo.Invite.builder()
                        .email("chaos@pr0gramm.com")
                        .name("cha0s")
                        .mark(3)
                        .created(Instant.now().minus(1000000))
                        .build())
                .build());
    }

    public static class InviteException extends RuntimeException {
        private final String errorCode;

        public InviteException(String errorCode) {
            this.errorCode = errorCode;
        }

        public boolean noMoreInvites() {
            return ERROR_INVITES.equalsIgnoreCase(errorCode);
        }

        public boolean emailFormat() {
            return ERROR_EMAIL_FORMAT.equalsIgnoreCase(errorCode);
        }

        public boolean emailInUse() {
            return ERROR_IN_USE.equalsIgnoreCase(errorCode);
        }
    }

    @Value.Immutable
    public interface Invites {
        int inviteCount();

        List<AccountInfo.Invite> invited();
    }

    public static final String ERROR_EMAIL_FORMAT = "emailInvalid";
    public static final String ERROR_INVITES = "noInvites";
    public static final String ERROR_IN_USE = "emailInUse";
}

@@ -164,76 +164,8 @@
 nfo;
-%0Aimport com.pr0gramm.app.api.pr0gramm.response.ImmutableAccountInfo;
 %0A%0Aim
@@ -200,38 +200,8 @@
 lue;
-%0Aimport org.joda.time.Instant;
 %0A%0Aim
@@ -1198,24 +1198,26 @@
 .invited())%0A
+//
             
@@ -1262,32 +1262,34 @@
 nvite.builder()%0A
+//
                 
@@ -1307,17 +1307,17 @@
 ail(%22cha
-o
+0
 s@pr0gra
@@ -1325,16 +1325,18 @@
 m.com%22)%0A
+//
         
@@ -1366,16 +1366,18 @@
 cha0s%22)%0A
+//
         
@@ -1401,16 +1401,18 @@
 mark(3)%0A
+//
         
@@ -1466,16 +1466,18 @@
 00000))%0A
+//
         

Remove cha0s from invites.