/*
 * Copyright (C) 2018 Peter Gregus for GravityBox Project (C3C076@xda)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ceco.r.gravitybox.managers;

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

import com.ceco.r.gravitybox.GravityBox;
import com.ceco.r.gravitybox.GravityBoxService;
import com.ceco.r.gravitybox.Utils;
import com.ceco.r.gravitybox.ledcontrol.QuietHours;
import com.ceco.r.gravitybox.ledcontrol.QuietHoursActivity;

import de.robv.android.xposed.XSharedPreferences;
import android.content.Context;
import android.content.Intent;

public class SysUiStatusbarQuietHoursManager implements BroadcastMediator.Receiver {
    private static final String TAG = "GB:StatusbarQuietHoursManager";
    private static final Object lock = new Object();
    private static SysUiStatusbarQuietHoursManager sManager;

    private Context mContext;
    private QuietHours mQuietHours;
    private List<QuietHoursListener> mListeners;

    public interface QuietHoursListener {
        void onQuietHoursChanged();
        void onTimeTick();
    }

    protected static SysUiStatusbarQuietHoursManager getInstance(Context context, XSharedPreferences qhPrefs) {
        synchronized(lock) {
            if (sManager == null) {
                sManager = new SysUiStatusbarQuietHoursManager(context, qhPrefs);
            }
            return sManager;
        }
    }

    private SysUiStatusbarQuietHoursManager(Context context, XSharedPreferences qhPrefs) {
        mContext = context;
        mQuietHours = new QuietHours(qhPrefs);
        mListeners = new ArrayList<>();

        SysUiManagers.BroadcastMediator.subscribe(this,
                Intent.ACTION_TIME_TICK,
                Intent.ACTION_TIME_CHANGED,
                Intent.ACTION_TIMEZONE_CHANGED,
                QuietHoursActivity.ACTION_QUIET_HOURS_CHANGED);
    }

    @Override
    public void onBroadcastReceived(Context context, Intent intent) {
        final String action = intent.getAction();
        if (action.equals(Intent.ACTION_TIME_TICK) ||
                action.equals(Intent.ACTION_TIME_CHANGED) ||
                action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
            notifyTimeTick();
        } else if (action.equals(QuietHoursActivity.ACTION_QUIET_HOURS_CHANGED)) {
            mQuietHours = new QuietHours(intent.getExtras());
            notifyQuietHoursChange();
        }
    }

    public void registerListener(QuietHoursListener listener) {
        if (listener == null) return;

        if (!mListeners.contains(listener)) {
            mListeners.add(listener);
        }
    }

    public void unregisterListener(QuietHoursListener listener) {
        if (listener == null) return;

        mListeners.remove(listener);
    }

    private void notifyTimeTick() {
        for (QuietHoursListener l : mListeners) {
            l.onTimeTick();
        }
    }

    private void notifyQuietHoursChange() {
        for (QuietHoursListener l : mListeners) {
            l.onQuietHoursChanged();
        }
    }

    public QuietHours getQuietHours() {
        return mQuietHours;
    }

    public void setMode(QuietHours.Mode mode) {
        try {
            Context gbContext = Utils.getGbContext(mContext);
            Intent intent = new Intent(gbContext, GravityBoxService.class);
            intent.setAction(QuietHoursActivity.ACTION_SET_QUIET_HOURS_MODE);
            intent.putExtra(QuietHoursActivity.EXTRA_QH_MODE, mode.toString());
            gbContext.startService(intent);
        } catch (Throwable t) {
            GravityBox.log(TAG, t);
        }
    }
}
