package net.miz_hi.smileessence.talkchase;

import net.miz_hi.smileessence.cache.TweetCache;
import net.miz_hi.smileessence.model.status.tweet.TweetModel;
import net.miz_hi.smileessence.model.statuslist.StatusList;
import net.miz_hi.smileessence.model.statuslist.impl.TalkList;
import net.miz_hi.smileessence.notification.Notificator;
import net.miz_hi.smileessence.status.TweetUtils;
import net.miz_hi.smileessence.statuslist.StatusListManager;
import net.miz_hi.smileessence.view.fragment.impl.TalkFragment;

public class TalkChaser
{

    public TalkFragment fragment;
    public long chasingId;
    public StatusList talkList;

    public TalkChaser(TalkFragment fragment)
    {
        this.talkList = new TalkList();
        this.fragment = fragment;
    }

    public void hitNewTweet(TweetModel tweet)
    {
        this.talkList.addToTop(tweet.getOriginal());
        updateChasingId(tweet.getOriginal().statusId);
    }

    private void updateChasingId(long id)
    {
        this.chasingId = id;
        fragment.setChasingId(chasingId);
    }

    /**
     * Call on Network Thread
     */
    public void startRelation(long statusId)
    {
        TweetModel start = TweetUtils.getOrCreateStatusModel(statusId);
        if (start == null)
        {
            Notificator.alert("ツイートの取得に失敗しました");
            return;
        }
        talkList.addToTop(start);

        //Load older
        long inReplyTo = start.getInReplyToStatusId();
        while (inReplyTo > 0)
        {
            TweetModel older = TweetUtils.getOrCreateStatusModel(inReplyTo);
            talkList.addToBottom(older);
            inReplyTo = older.getInReplyToStatusId();
        }

        //Load newer
        inReplyTo = start.statusId;
        while (true)
        {
            long before = inReplyTo;
            for (TweetModel newer : TweetCache.getList())
            {
                if (newer.getInReplyToStatusId() == inReplyTo)
                {
                    talkList.addToTop(newer);
                    inReplyTo = newer.statusId;
                    break;
                }
            }
            if (before == inReplyTo)
            {
                break;
            }
        }

        updateChasingId(((TweetModel) talkList.getStatus(0)).statusId);
        StatusListManager.getAdapter(talkList).forceNotifyAdapter();
    }

    public void stopRelation()
    {
        TalkManager.removeTalkChaser(this);
    }
}

@@ -1563,16 +1563,151 @@
 plyTo);%0A
+            if (older == null)%0A            %7B%0A                Notificator.alert(%22%E3%83%84%E3%82%A4%E3%83%BC%E3%83%88%E3%81%AE%E5%8F%96%E5%BE%97%E3%81%AB%E5%A4%B1%E6%95%97%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F%22);%0A                break;%0A            %7D%0A
         

Fix talk chasing : crash when get protected user's tweet