/*
 * Copyright (c) 2022 Proton AG
 *
 * This file is part of Proton Mail.
 *
 * Proton Mail is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Proton Mail is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Proton Mail. If not, see https://www.gnu.org/licenses/.
 */
package ch.protonmail.android.jobs;

import com.birbit.android.jobqueue.Params;

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

import ch.protonmail.android.api.models.IDList;
import ch.protonmail.android.core.Constants;
import ch.protonmail.android.data.local.CounterDao;
import ch.protonmail.android.data.local.CounterDatabase;
import ch.protonmail.android.data.local.model.Message;
import ch.protonmail.android.data.local.model.UnreadLocationCounter;
import ch.protonmail.android.utils.ServerTime;
import timber.log.Timber;

public class PostReadJob extends ProtonMailEndlessJob {

    private final List<String> mMessageIds;

    public PostReadJob(final List<String> messageIds) {
        super(new Params(Priority.HIGH).requireNetwork().persist().groupBy(Constants.JOB_GROUP_MESSAGE));
        mMessageIds = messageIds;
    }

    @Override
    public void onAdded() {
        Timber.d("markRead %s", mMessageIds);
        final CounterDao counterDao = CounterDatabase.Companion
                .getInstance(getApplicationContext(), getUserId())
                .getDao();
        Constants.MessageLocationType messageLocation = Constants.MessageLocationType.INVALID;
        boolean starred = false;
        for (String id : mMessageIds) {
            final Message message = getMessageDetailsRepository().findMessageByIdBlocking(id);
            if (message != null) {
                starred = message.isStarred() != null && message.isStarred();
                messageLocation = Constants.MessageLocationType.Companion.fromInt(message.getLocation());
                message.setAccessTime(ServerTime.currentTimeMillis());
                message.setIsRead(true);
                getMessageDetailsRepository().saveMessageBlocking(message);
            }
        }

        if (messageLocation != Constants.MessageLocationType.INVALID) {
            UnreadLocationCounter unreadLocationCounter = counterDao.findUnreadLocationByIdBlocking(messageLocation.getMessageLocationTypeValue());
            if (unreadLocationCounter == null) {
                return;
            }
            unreadLocationCounter.decrement(mMessageIds.size());
            List<UnreadLocationCounter> countersToUpdate = new ArrayList<>();
            countersToUpdate.add(unreadLocationCounter);
            if (starred) {
                UnreadLocationCounter starredUnread = counterDao.findUnreadLocationByIdBlocking(Constants.MessageLocationType.STARRED.getMessageLocationTypeValue());
                if (starredUnread != null) {
                    starredUnread.decrement();
                    countersToUpdate.add(starredUnread);
                }
            }
            counterDao.insertAllUnreadLocations(countersToUpdate);
        }
    }

    @Override
    public void onRun() throws Throwable {
        try {
            List<String> messageIds = new ArrayList<>(mMessageIds);
            getApi().markMessageAsRead(new IDList(messageIds));
        } catch (Exception e) {
            Timber.i(e);
        }
    }
}
