package com.casic.dcms.utils;

import com.casic.dcms.base.BaseApplication;
import com.casic.dcms.bean.MapConfigBean;
import com.casic.dcms.bean.NoticeLocaleBean;
import com.casic.dcms.bean.ToiletListBean;
import com.casic.dcms.greendao.MapConfigBeanDao;
import com.casic.dcms.greendao.NoticeLocaleBeanDao;
import com.casic.dcms.greendao.ToiletListBeanDao;
import com.casic.dcms.model.MapConfigModel;
import com.casic.dcms.model.ToiletListModel;
import com.casic.dcms.view.MainActivity;

import java.util.ArrayList;
import java.util.List;

public class DataBaseManager {

    private DataBaseManager() {
    }

    private static final class DataBaseManagerHolder {
        static final DataBaseManager INSTANCE = new DataBaseManager();
    }

    /**
     * 双重锁单例
     */
    public static DataBaseManager get() {
        return DataBaseManagerHolder.INSTANCE;
    }

    private final NoticeLocaleBeanDao noticeBeanDao = BaseApplication.getDaoSession().getNoticeLocaleBeanDao();
    private final MapConfigBeanDao mapConfigBeanDao = BaseApplication.getDaoSession().getMapConfigBeanDao();
    private final ToiletListBeanDao toiletListBeanDao = BaseApplication.getDaoSession().getToiletListBeanDao();

    /***通知本地数据管理************************************************************/
    public void savePushNotice(NoticeLocaleBean noticeBean) {
        noticeBeanDao.save(noticeBean);
        //角标设置
        BadeHelper.setBadgeNum(BaseApplication.get(), queryUnReadNotice());
        //更新底部导航消息数量
        MainActivity.weakReferenceHandler.sendEmptyMessage(LocaleConstant.MESSAGE_NUMBER);
    }

    public int queryUnReadNotice() {
        return noticeBeanDao.queryBuilder().where(
                NoticeLocaleBeanDao.Properties.IsRead.eq("1")
        ).list().size();
    }

    public List<NoticeLocaleBean> queryNoticeByUser(String userId) {
        return noticeBeanDao.queryBuilder()
                .where(NoticeLocaleBeanDao.Properties.UserId.eq(userId))
                .orderDesc(NoticeLocaleBeanDao.Properties.PushDate)
                .list();
    }

    public void deleteNoticeByMessageId(String messageId) {
        List<NoticeLocaleBean> result = noticeBeanDao.queryBuilder().where(
                NoticeLocaleBeanDao.Properties.MessageId.eq(messageId)
        ).list();
        noticeBeanDao.deleteInTx(result);
        //角标设置
        BadeHelper.setBadgeNum(BaseApplication.get(), queryUnReadNotice());
        //更新底部导航消息数量
        MainActivity.weakReferenceHandler.sendEmptyMessage(LocaleConstant.MESSAGE_NUMBER);
    }

    public void updateNoticeByMessageId(String messageId) {
        NoticeLocaleBean noticeLocaleBean = noticeBeanDao.queryBuilder().where(
                NoticeLocaleBeanDao.Properties.MessageId.eq(messageId)
        ).unique();
        noticeLocaleBean.setIsRead("0");
        noticeBeanDao.update(noticeLocaleBean);
        //角标设置
        BadeHelper.setBadgeNum(BaseApplication.get(), queryUnReadNotice());
        //更新底部导航消息数量
        MainActivity.weakReferenceHandler.sendEmptyMessage(LocaleConstant.MESSAGE_NUMBER);
    }


    /***地图本地数据管理************************************************************/
    public void deleteAllMapConfig() {
        mapConfigBeanDao.deleteAll();
    }

    public void saveMapConfig(MapConfigModel.DataBean.RowsBean rowsBean) {
        MapConfigBean mapConfigBean = new MapConfigBean();
        mapConfigBean.setMapId(rowsBean.getId());
        mapConfigBean.setName(rowsBean.getName());
        mapConfigBean.setUrl(rowsBean.getUrl());
        mapConfigBeanDao.save(mapConfigBean);
    }

    public List<MapConfigModel.DataBean.RowsBean> getMapConfigList() {
        List<MapConfigModel.DataBean.RowsBean> result = new ArrayList<>();
        List<MapConfigBean> configBeans = mapConfigBeanDao.loadAll();
        for (MapConfigBean configBean : configBeans) {
            MapConfigModel.DataBean.RowsBean rowsBean = new MapConfigModel.DataBean.RowsBean();
            rowsBean.setId(configBean.getMapId());
            rowsBean.setName(configBean.getName());
            rowsBean.setUrl(configBean.getUrl());
            result.add(rowsBean);
        }
        return result;
    }

    /***公厕本地数据管理************************************************************/
    public void deleteAllToiletList() {
        toiletListBeanDao.deleteAll();
    }

    public void saveToilets(ToiletListModel.DataBean dataBean) {
        ToiletListBean bean = new ToiletListBean();
        bean.setArea(dataBean.getArea());
        bean.setCode(dataBean.getCode());
        bean.setCreateTime(dataBean.getCreateTime());
        bean.setDelFlag(dataBean.getDelFlag());
        bean.setDeptId(dataBean.getDeptId());
        bean.setToiletId(dataBean.getId());
        bean.setLat(dataBean.getLat());
        bean.setLng(dataBean.getLng());
        bean.setName(dataBean.getName());
        bean.setNotes(dataBean.getNotes());
        bean.setOwner(dataBean.getOwner());
        bean.setPhoto(dataBean.getPhoto());
        bean.setPosition(dataBean.getPosition());
        bean.setResponsiblePerson(dataBean.getResponsiblePerson());
        bean.setResponsiblePersonTel(dataBean.getResponsiblePersonTel());
        bean.setTs(dataBean.getTs());
        bean.setUpdateTime(dataBean.getUpdateTime());
        toiletListBeanDao.save(bean);
    }

    public List<ToiletListModel.DataBean> getToilets() {
        List<ToiletListModel.DataBean> result = new ArrayList<>();
        List<ToiletListBean> listBeans = toiletListBeanDao.loadAll();
        for (ToiletListBean toiletBean : listBeans) {
            ToiletListModel.DataBean dataBean = new ToiletListModel.DataBean();
            dataBean.setArea(toiletBean.getArea());
            dataBean.setCode(toiletBean.getCode());
            dataBean.setCreateTime(toiletBean.getCreateTime());
            dataBean.setDelFlag(toiletBean.getDelFlag());
            dataBean.setDeptId(toiletBean.getDeptId());
            dataBean.setId(toiletBean.getToiletId());
            dataBean.setLat(toiletBean.getLat());
            dataBean.setLng(toiletBean.getLng());
            dataBean.setName(toiletBean.getName());
            dataBean.setNotes(toiletBean.getNotes());
            dataBean.setOwner(toiletBean.getOwner());
            dataBean.setPhoto(toiletBean.getPhoto());
            dataBean.setPosition(toiletBean.getPosition());
            dataBean.setResponsiblePerson(toiletBean.getResponsiblePerson());
            dataBean.setResponsiblePersonTel(toiletBean.getResponsiblePersonTel());
            dataBean.setTs(toiletBean.getTs());
            dataBean.setUpdateTime(toiletBean.getUpdateTime());
            result.add(dataBean);
        }
        return result;
    }
}
