/*
 * Copyright 2016 The twicalico authors
 *
 * 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.
 */

package com.github.moko256.twicalico.model.impl;

import android.content.ContentResolver;
import android.net.Uri;

import com.github.moko256.twicalico.model.base.PostTweetModel;
import com.twitter.Validator;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import rx.Single;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import twitter4j.GeoLocation;
import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;

/**
 * Created by moko256 on 2017/07/22.
 *
 * @author moko256
 */

public class PostTweetModelImpl implements PostTweetModel {

    private Twitter twitter;
    private ContentResolver contentResolver;

    private long inReplyToStatusId = -1;
    private boolean possiblySensitive;
    private String tweetText;
    private List<Uri> uriList;
    private GeoLocation location;

    private Validator validator = new Validator();

    public PostTweetModelImpl(Twitter twitter, ContentResolver contentResolver){
        this.twitter = twitter;
        this.contentResolver = contentResolver;
    }

    @Override
    public long getInReplyToStatusId() {
        return inReplyToStatusId;
    }

    @Override
    public void setInReplyToStatusId(long inReplyToStatusId) {
        this.inReplyToStatusId = inReplyToStatusId;
    }

    @Override
    public boolean isPossiblySensitive() {
        return possiblySensitive;
    }

    @Override
    public void setPossiblySensitive(boolean possiblySensitive) {
        this.possiblySensitive = possiblySensitive;
    }

    @Override
    public String getTweetText() {
        return tweetText;
    }

    @Override
    public void setTweetText(String tweetText) {
        this.tweetText = tweetText;
    }

    @Override
    public int getTweetLength() {
        return validator.getTweetLength(tweetText);
    }

    @Override
    public boolean isReply() {
        return inReplyToStatusId != -1;
    }

    @Override
    public boolean isValidTweet() {
        return validator.isValidTweet(tweetText);
    }

    @Override
    public List<Uri> getUriList() {
        return uriList;
    }

    @Override
    public void setUriList(List<Uri> uriList) {
        this.uriList = uriList;
    }

    @Override
    public GeoLocation getLocation() {
        return location;
    }

    @Override
    public void setLocation(GeoLocation location) {
        this.location = location;
    }

    @Override
    public Single<Status> postTweet() {
        Single<Status> single = Single.create(subscriber -> {
            if (isValidTweet()){
                try {
                    StatusUpdate statusUpdate = new StatusUpdate(tweetText);
                    if (uriList != null && uriList.size() > 0) {
                        long ids[] = new long[uriList.size()];
                        for (int i = 0; i < uriList.size(); i++) {
                            Uri uri = uriList.get(i);
                            InputStream image = contentResolver.openInputStream(uri);
                            ids[i] = twitter.uploadMedia(uri.getLastPathSegment(), image).getMediaId();
                        }
                        statusUpdate.setMediaIds(ids);
                        statusUpdate.setPossiblySensitive(possiblySensitive);
                    }
                    if (isReply()){
                        statusUpdate.setInReplyToStatusId(inReplyToStatusId);
                    }
                    if (location != null){
                        statusUpdate.setLocation(location);
                    }
                    subscriber.onSuccess(twitter.updateStatus(statusUpdate));
                } catch (FileNotFoundException | TwitterException e){
                    subscriber.onError(e);
                }
            } else {
                subscriber.onError(new PostTweetModelImpl.InvalidTextException());
            }
        });
        return single
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
    }

    private static class InvalidTextException extends Throwable {
        InvalidTextException(){
            super("Invalid length of tweet text");
        }
    }
}

@@ -2657,32 +2657,75 @@
 %7B%0A        return
+ (uriList != null && uriList.size() %3E 0) %7C%7C
  validator.isVal
@@ -3245,45 +3245,8 @@
 %3E %7B%0A
-            if (isValidTweet())%7B%0A    
     
@@ -3259,20 +3259,16 @@
   try %7B%0A
-    
         
@@ -3332,20 +3332,16 @@
 tText);%0A
-    
         
@@ -3413,20 +3413,16 @@
         
-    
 long ids
@@ -3452,20 +3452,16 @@
 ize()%5D;%0A
-    
         
@@ -3539,20 +3539,16 @@
         
-    
 Uri uri 
@@ -3589,20 +3589,16 @@
         
-   
- 
 InputStr
@@ -3643,28 +3643,24 @@
 tream(uri);%0A
-    
             
@@ -3763,34 +3763,26 @@
             
-   
- 
 %7D%0A
-    
             
@@ -3832,36 +3832,32 @@
                 
-  
-  
 statusUpdate.set
@@ -3906,32 +3906,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -3924,26 +3924,24 @@
             
-  
 if (isReply(
@@ -3944,20 +3944,16 @@
 ply())%7B%0A
-    
         
@@ -4030,34 +4030,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -4087,36 +4087,32 @@
                 
-    
 statusUpdate.set
@@ -4131,20 +4131,16 @@
 ation);%0A
-    
         
@@ -4157,36 +4157,32 @@
                 
-    
 subscriber.onSuc
@@ -4219,28 +4219,24 @@
 usUpdate));%0A
-    
             
@@ -4297,36 +4297,32 @@
                 
-    
 subscriber.onErr
@@ -4332,130 +4332,8 @@
 e);%0A
-                %7D%0A            %7D else %7B%0A                subscriber.onError(new PostTweetModelImpl.InvalidTextException());%0A
     
@@ -4500,171 +4500,6 @@
  %7D%0A%0A
-    private static class InvalidTextException extends Throwable %7B%0A        InvalidTextException()%7B%0A            super(%22Invalid length of tweet text%22);%0A        %7D%0A    %7D%0A
 %7D%0A

fix valid tweet checker