import math
from tqdm import tqdm
from dbfread import DBF
from utils.logger import setup_logger

logger = setup_logger()


class BoreholeClient:
    """
    Data dictionary: This description is based on the information presented in the following
    Geological articles:
    https://nora.nerc.ac.uk/id/eprint/509366/1/IR04115.pdf
    https://shop.bgs.ac.uk/Resources/Shop/doc/info/Borehole_Abbreviations.pdf?_ga=2.246788941.895115819.1686912089
    -542796874.1686912089
    https://core.ac.uk/download/63732.pdf


    QS - Borehole identifier information
    NUMB - Borehole identifier information
    BSUFF - Borehole identifier information
    REGNO
    RT - Borehole identifier information
    GRID_REFER
    EASTING - British National Grid coordinates
    NORTHING - British National Grid coordinates
    X - British National Grid coordinates - same as EASTING but has a float typing
    Y - British National Grid coordinates - same as NORTHING but has a float typing
    CONFIDENTI
    STRTHEIGHT
    NAME
    LENGTH
    BGS_ID
    DATE_KNOWN
    DATE_K_TYP
    DATE_ENTER
    AGS_LOG_UR
    """

    def __init__(self, path):
        self.path: str = path
        self.data = None

    def read(self):
        logger.info("Reading in borehole data")
        table = DBF(self.path)
        borehole_data = [x for x in tqdm(table, total=len(table))]
        self.data = borehole_data

    @staticmethod
    def distance_between_bng_coords(x1_bng, y1_bng, x2_bng, y2_bng):
        # Calculate the Euclidean distance between the points
        distance_m = math.sqrt((x2_bng - x1_bng) ** 2 + (y2_bng - y1_bng) ** 2)
        distance_km = distance_m / 1000  # convert meters to kilometers
        return distance_m, distance_km

    # EXAMPLE
    # There are ~1.4 million entries in this dataset and so we firstly want to reduce the number of
    # entries in here if possible before we produce any form of comparison between our epc, to infer
    # the distance from the property to the nearest borehole

    # Let's take a sample
    # borehold_compare_to = borehole_client.data[0]
    # property = input_properties[0]
    #
    # # for each property, find the nearest borehole
    # # This is just an example, looking at the distance from a property to a borehole
    # dist_m, dist_km = borehole_client.distance_between_bng_coords(
    #     x1_bng=property.coordinates["x_coordinate"],
    #     y1_bng=property.coordinates["y_coordinate"],
    #     x2_bng=borehold_compare_to["X"],
    #     y2_bng=borehold_compare_to["Y"],
    # )
