package ai.pai.lensman.briefs;

import android.os.AsyncTask;

import com.android.common.executors.ThreadExecutor;
import com.android.common.utils.JSONParseUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import ai.pai.lensman.base.BaseInteractor;
import ai.pai.lensman.bean.BriefsBean;
import ai.pai.lensman.bean.GroupPhotoItem;
import ai.pai.lensman.bean.MessageBean;
import ai.pai.lensman.bean.SystemMessageBean;
import ai.pai.lensman.db.Preferences;
import ai.pai.lensman.utils.HttpPostTask;
import ai.pai.lensman.utils.UrlContainer;

/**
 * Created by chengzhenyu on 2016/8/24.
 */

public class BriefsInteractor implements BaseInteractor {

    private HttpPostTask fetchInfoTask;
    private InteractorListener<BriefsBean> listener;

    public BriefsInteractor(InteractorListener<BriefsBean> listener){
        this.listener = listener;
    }

    @Override
    public void startJob() {
        HashMap<String,String> params = new HashMap<>();
        params.put("user_id", Preferences.getInstance().getLensManId());
        fetchInfoTask = new HttpPostTask(params){

            String message;
            BriefsBean bean;

            @Override
            protected boolean parseResponse(String response) {
                try{
                    JSONObject json = new JSONObject(response);
                    int status = json.getInt("status");
                    if(status == 200){
                        JSONObject data = json.getJSONObject("data");
                        bean = new BriefsBean();
                        bean.weekIncome =(float)data.getDouble("week_income");
                        bean.todayIncome = (float)data.getDouble("today_income");
                        bean.todayUpload = data.getInt("today_upload");
                        bean.weekSold = data.getInt("week_sold");
                        JSONObject msgObj = data.getJSONObject("messages");
                        JSONArray systemMsgArray = msgObj.getJSONArray("system");

                        if(systemMsgArray!=null && systemMsgArray.length()>0){
                            ArrayList<SystemMessageBean> systemMsgList = new ArrayList<>();
                            for(int k = 0 ;k<systemMsgArray.length();k++){
                                JSONObject systemMsgObj = systemMsgArray.getJSONObject(k);
                                SystemMessageBean bean = new SystemMessageBean();
                                bean.content = JSONParseUtils.getJSONString(systemMsgObj.getString("content"));
                                bean.isRead = systemMsgObj.getBoolean("read");
                                bean.url = JSONParseUtils.getJSONString(systemMsgObj.getString("url"));
                                bean.msgId =JSONParseUtils.getJSONString(systemMsgObj.getString("pk"));
                                bean.title = JSONParseUtils.getJSONString(systemMsgObj.getString("title"));
                                systemMsgList.add(bean);
                            }
                            bean.systemMsgList = systemMsgList;
                        }
                        JSONArray msgArray = msgObj.getJSONArray("orders");
                        if(msgArray!=null && msgArray.length()>0){
                            ArrayList<MessageBean> msgList = new ArrayList<>();
                            for(int count = 0; count<msgArray.length();count++){
                                JSONObject childMsg = msgArray.getJSONObject(count);
                                MessageBean childBean = new MessageBean();
                                childBean.createTime = JSONParseUtils.getJSONString(childMsg.getString("created_at"));
                                childBean.isRead = childMsg.getBoolean("read");
                                childBean.msgId = childMsg.getString("pk");
                                childBean.senderName = JSONParseUtils.getJSONString(childMsg.getString("from_nickname"));
                                childBean.senderAvatarUrl = JSONParseUtils.getJSONString(childMsg.getString("from_avatar"));
                                childBean.content = JSONParseUtils.getJSONString(childMsg.getString("msg_content"));
                                JSONObject photoObj = childMsg.getJSONObject("group_photo_info");
                                childBean.photoItem = GroupPhotoItem.parseFromJSON(photoObj);
                                msgList.add(childBean);
                            }
                            bean.msgList = msgList;
                        }
                        return true;
                    }else{
                        message = json.getString("message");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                return false;
            }

            @Override
            protected void onPostFail() {
                super.onPostFail();
                listener.onInteractFail(message);
            }

            @Override
            protected void onPostSuccess() {
                super.onPostSuccess();
                listener.onInteractSuccess(bean);
            }
        };
        fetchInfoTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.BRIEFS_URL);
    }

    @Override
    public void cancelJob() {
        if(fetchInfoTask==null){
            return;
        }
        if(fetchInfoTask.getStatus()== AsyncTask.Status.RUNNING){
            fetchInfoTask.cancel(true);
        }
        fetchInfoTask = null;
    }
}
