/*-
 * ~~~~~~licensing~~~~~~
 * core-support-runtime
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.core.support.runtime.cluster;

import java.util.concurrent.TimeUnit;

import org.apache.camel.cluster.CamelClusterView;
import org.apache.camel.support.cluster.AbstractCamelClusterService;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;


@Deprecated(since = "1.10")
public class HazelcastLockClusterService extends AbstractCamelClusterService<CamelClusterView> {
    
    private static final String DEFAULT_CACHE_NAME = "entaxy";
    private static final String DEFAULT_ID = "entaxy";
    
    private HazelcastInstance hazelcastInstance;
    private IMap<String,Object> cache;
    private String cacheName;
    
    private long acquireLockDelay = 1;
    private TimeUnit acquireLockDelayUnit = TimeUnit.SECONDS;
    private long acquireLockInterval = 10;
    private TimeUnit acquireLockIntervalUnit = TimeUnit.SECONDS;
    private long healthInterval = 60;
    private TimeUnit healthIntervalUnit = TimeUnit.SECONDS;

    public HazelcastLockClusterService(HazelcastInstance hazelcastInstance) {
        this(hazelcastInstance, DEFAULT_CACHE_NAME);
    }
    
    public HazelcastLockClusterService(HazelcastInstance hazelcastInstance, String cacheName) {
        this.hazelcastInstance = hazelcastInstance;
        this.cacheName = cacheName;
        this.cache = hazelcastInstance.getMap(cacheName);
        setId(DEFAULT_ID);
    }
    
    @Override
    protected CamelClusterView createView(String namespace) throws Exception {
        return new HazelcastValueLockClusterView(this, namespace);
    }

    public String getCacheName() {
        return cacheName;
    }

    public void setCacheName(String cacheName) {
        this.cacheName = cacheName;
    }

    public HazelcastInstance getHazelcastInstance() {
        return hazelcastInstance;
    }

    public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
        this.hazelcastInstance = hazelcastInstance;
    }

    public IMap<String, Object> getCache() {
        return cache;
    }

    public void setCache(IMap<String, Object> cache) {
        this.cache = cache;
    }

    public long getAcquireLockDelay() {
        return acquireLockDelay;
    }

    /**
     * The time to wait before starting to try to acquire lock, default 1.
     */
    public void setAcquireLockDelay(long acquireLockDelay) {
        this.acquireLockDelay = acquireLockDelay;
    }

    public void setAcquireLockDelay(long pollDelay, TimeUnit pollDelayUnit) {
        setAcquireLockDelay(pollDelay);
        setAcquireLockDelayUnit(pollDelayUnit);
    }

    public TimeUnit getAcquireLockDelayUnit() {
        return acquireLockDelayUnit;
    }

    /**
     * The time unit of the acquireLockDelay, default to TimeUnit.SECONDS.
     */
    public void setAcquireLockDelayUnit(TimeUnit acquireLockDelayUnit) {
        this.acquireLockDelayUnit = acquireLockDelayUnit;
    }

    public long getAcquireLockInterval() {
        return acquireLockInterval;
    }

    /**
     * The time to wait between attempts to try to acquire lock, default 10.
     */
    public void setAcquireLockInterval(long acquireLockInterval) {
        this.acquireLockInterval = acquireLockInterval;
    }

    public void setAcquireLockInterval(long pollInterval, TimeUnit pollIntervalUnit) {
        setAcquireLockInterval(pollInterval);
        setAcquireLockIntervalUnit(pollIntervalUnit);
    }

    public TimeUnit getAcquireLockIntervalUnit() {
        return acquireLockIntervalUnit;
    }

    /**
     * The time unit of the acquireLockInterval, default to TimeUnit.SECONDS.
     */
    public void setAcquireLockIntervalUnit(TimeUnit acquireLockIntervalUnit) {
        this.acquireLockIntervalUnit = acquireLockIntervalUnit;
    }

    public long getHealthInterval() {
        return healthInterval;
    }
    
    /**
     * The time between current timestamp and leader's last visit, should be greater then acquireLockDelay+acquireLockInterval, default 60.
     */
    public void setHealthInterval(long healthInterval) {
        this.healthInterval = healthInterval;
    }

    public TimeUnit getHealthIntervalUnit() {
        return healthIntervalUnit;
    }

    /**
     * The time unit of the healthInterval, default to TimeUnit.SECONDS.
     */
    public void setHealthIntervalUnit(TimeUnit healthIntervalUnit) {
        this.healthIntervalUnit = healthIntervalUnit;
    }
}
