package com.tofly.ghrq_route.module.message;

import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.job.NotifyMsgDaoHelper;
import com.tofly.ghrq_route.module.message.domain.InMessageBean;
import com.tofly.ghrq_route.module.notifyManage.NotifyManageBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;

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

/**
 * Created by Amu on 2017/4/26.
 */

public class MessageTool {

    //private static final String TAG = TroubleTool.class.getName();

    private static MessageTool messageTool = null;

    private NotifyMsgDaoHelper notifyMsgDaoHelper;

    private MessageTool(){
        notifyMsgDaoHelper = NotifyMsgDaoHelper.getInstance();
    }

    public static synchronized MessageTool getInstance(){
        if (messageTool == null){
            messageTool = new MessageTool();
        }
        return messageTool;
    }

    /*本地保存未通知用户的未读消息数据*/
    public void saveUnNotifyMessage(List<InMessageBean> messageList) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (messageList != null && userBean != null) {
            for (InMessageBean messageBean : messageList) {
                String messageId = messageBean.getMessageId();
                NotifyManageBean notifyManageBean = getNotifyMsgBean(messageId);
                if (notifyManageBean == null) {
                    notifyManageBean = new NotifyManageBean();
                    String id = UUID.randomUUID().toString();
                    notifyManageBean.setId(id);
                    notifyManageBean.setServerId(messageId);
                    notifyManageBean.setNotify_type(NotifyManageBean.NOTIFY_MSG_TYPE.NOTIFY_MSG_MESSAGE.getType());
                    notifyManageBean.setIsNotify(false);
                    notifyManageBean.setIsRead(false);
                    notifyManageBean.setUserid(userBean.getUserid());
                    notifyMsgDaoHelper.insertNotifyMsgBean(notifyManageBean);
                }
            }
        }
    }

    public NotifyManageBean getNotifyMsgBean(String messageId) {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            return notifyMsgDaoHelper.getMessageNotifyMsgBean(userBean.getUserid(), messageId);
        }
        return null;
    }

    public List<NotifyManageBean> getUnNotifyMsgBean() {
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            return notifyMsgDaoHelper.getMessageUnNotifyMsgBean(userBean.getUserid());
        }
        return null;
    }

    public void setMsgBeanNotified(List<NotifyManageBean> msgBeans) {
        notifyMsgDaoHelper.setMsgBeanNotified(msgBeans);
    }

    public void setMsgBeanRead(NotifyManageBean msgBean) {
        notifyMsgDaoHelper.setMsgBeanRead(msgBean);
    }
}
