package net.qiujuer.powerback.ankey.presenter;

import net.qiujuer.genius.kit.util.Tools;
import net.qiujuer.genius.kit.util.UiKit;
import net.qiujuer.powerback.ankey.model.db.InfoModel;
import net.qiujuer.powerback.ankey.model.view.InfoViewModel;
import net.qiujuer.powerback.ankey.presenter.view.InfoListView;

import java.util.List;
import java.util.UUID;

/**
 * Created by qiujuer
 * on 15/10/29.
 */
public class InfoListPresenter {
    private InfoListView mView;
    private Thread mThread;

    public InfoListPresenter(InfoListView view) {
        mView = view;
    }

    public void refresh() {
        if (mThread != null)
            return;

        startLoad();
        mThread = new Thread("InfoListPresenter-Thread") {
            @Override
            public void run() {
                Tools.sleepIgnoreInterrupt(200);
                loadData();
                mThread = null;
            }
        };
        mThread.start();
    }

    private void loadData() {
        // load data
        List<InfoModel> models = InfoModel.getAll();
        if (models == null || models.size() == 0) {
            stopLoad(false);
        } else {
            stopLoad(true);
            List<InfoViewModel> viewModels = mView.getDataSet();
            for (InfoModel model : models) {
                if (!formatModel(viewModels, model))
                    break;
            }
        }
    }

    private boolean formatModel(List<InfoViewModel> viewModels, InfoModel model) {
        InfoViewModel sModel = searchViewModel(viewModels, model);
        if (sModel == null) {
            sModel = new InfoViewModel(model.getInfoId());
            viewModels.add(sModel);
        }
        return decryptModel(model, sModel);
    }

    private InfoViewModel searchViewModel(List<InfoViewModel> viewModels, InfoModel model) {
        UUID id = model.getInfoId();
        for (InfoViewModel viewModel : viewModels) {
            if (viewModel.getId().equals(id))
                return viewModel;
        }
        return null;
    }

    private void startLoad() {
        mView.hideNull();
        mView.showLoading();
    }

    private void stopLoad(final boolean haveData) {
        UiKit.runOnMainThreadAsync(new Runnable() {
            @Override
            public void run() {
                mView.hideLoading();
                if (haveData)
                    mView.hideNull();
                else
                    mView.showNull();
            }
        });
    }

    private boolean decryptModel(InfoModel src, InfoViewModel result) {
        try {
            result.setDescription(AppPresenter.decrypt(src.getDescription()));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void destroy() {

    }
}

@@ -41,16 +41,42 @@
 enter;%0A%0A
+import android.util.Log;%0A%0A
 import n
@@ -853,17 +853,18 @@
 terrupt(
-2
+10
 00);%0A   
@@ -1309,32 +1309,80 @@
 del : models) %7B%0A
+                Tools.sleepIgnoreInterrupt(20);%0A
                 
@@ -1468,32 +1468,504 @@
        %7D%0A    %7D%0A%0A
+    private void notifyInsert(final int index) %7B%0A        UiKit.runOnMainThreadAsync(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                mView.notifyItemChanged(index);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void notifyChange(final int index) %7B%0A        UiKit.runOnMainThreadAsync(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                mView.notifyItemChanged(index);%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     private bool
@@ -2094,24 +2094,58 @@
 ls, model);%0A
+        boolean isCreate = false;%0A
         if (
@@ -2269,52 +2269,771 @@
     
-%7D%0A        return decryptModel(model, sModel)
+    isCreate = true;%0A        %7D%0A        if (decryptModel(model, sModel)) %7B%0A            int index = getModelIndex(viewModels, model);%0A            Log.e(InfoListPresenter.class.getSimpleName(), viewModels.size() + %22 %22 + index + %22 %22 + isCreate);%0A            if (index %3E= 0) %7B%0A                if (isCreate)%0A                    notifyInsert(index);%0A                else%0A                    notifyChange(index);%0A            %7D%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A    private int getModelIndex(List%3CInfoViewModel%3E viewModels, InfoModel model) %7B%0A        int index = 0;%0A        for (InfoViewModel i : viewModels) %7B%0A            if (i.getId().equals(model.getInfoId()))%0A                return index;%0A            index++;%0A        %7D%0A        return -1
 ;%0A  

Improve info list load