# Copyright (c) Mengning Software. 2023. All rights reserved.
#
# Super IDE licensed under GNU Affero General Public License v3 (AGPL-3.0) .
# You can use this software according to the terms and conditions of the AGPL-3.0.
# You may obtain a copy of AGPL-3.0 at:
#
#    https://www.gnu.org/licenses/agpl-3.0.txt
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the AGPL-3.0 for more details.

import os
from time import sleep, time

from superide.exception import UserSideException

LOCKFILE_TIMEOUT = 3600  # in seconds, 1 hour
LOCKFILE_DELAY = 0.2

LOCKFILE_INTERFACE_FCNTL = 1
LOCKFILE_INTERFACE_MSVCRT = 2

try:
    import fcntl

    LOCKFILE_CURRENT_INTERFACE = LOCKFILE_INTERFACE_FCNTL
except ImportError:
    try:
        import msvcrt

        LOCKFILE_CURRENT_INTERFACE = LOCKFILE_INTERFACE_MSVCRT
    except ImportError:
        LOCKFILE_CURRENT_INTERFACE = None


class LockFileExists(UserSideException):
    pass


class LockFileTimeoutError(UserSideException):
    pass


class LockFile:
    def __init__(self, path, timeout=LOCKFILE_TIMEOUT, delay=LOCKFILE_DELAY):
        self.timeout = timeout
        self.delay = delay
        self._lock_path = os.path.abspath(path) + ".lock"
        self._fp = None

    def _lock(self):
        if not LOCKFILE_CURRENT_INTERFACE and os.path.exists(self._lock_path):
            # remove stale lock
            if time() - os.path.getmtime(self._lock_path) > 10:
                try:
                    os.remove(self._lock_path)
                except:  # pylint: disable=bare-except
                    pass
            else:
                raise LockFileExists

        self._fp = open(  # pylint: disable=consider-using-with
            self._lock_path, mode="w", encoding="utf8"
        )
        try:
            if LOCKFILE_CURRENT_INTERFACE == LOCKFILE_INTERFACE_FCNTL:
                fcntl.flock(self._fp.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
            elif LOCKFILE_CURRENT_INTERFACE == LOCKFILE_INTERFACE_MSVCRT:
                msvcrt.locking(  # pylint: disable=used-before-assignment
                    self._fp.fileno(), msvcrt.LK_NBLCK, 1
                )
        except (BlockingIOError, IOError) as exc:
            self._fp.close()
            self._fp = None
            raise LockFileExists from exc
        return True

    def _unlock(self):
        if not self._fp:
            return
        if LOCKFILE_CURRENT_INTERFACE == LOCKFILE_INTERFACE_FCNTL:
            fcntl.flock(self._fp.fileno(), fcntl.LOCK_UN)
        elif LOCKFILE_CURRENT_INTERFACE == LOCKFILE_INTERFACE_MSVCRT:
            msvcrt.locking(self._fp.fileno(), msvcrt.LK_UNLCK, 1)
        self._fp.close()
        self._fp = None

    def acquire(self):
        elapsed = 0
        while elapsed < self.timeout:
            try:
                return self._lock()
            except LockFileExists:
                sleep(self.delay)
                elapsed += self.delay

        raise LockFileTimeoutError()

    def release(self):
        self._unlock()
        if os.path.exists(self._lock_path):
            try:
                os.remove(self._lock_path)
            except:  # pylint: disable=bare-except
                pass

    def __enter__(self):
        self.acquire()

    def __exit__(self, type_, value, traceback):
        self.release()

    def __del__(self):
        self.release()
