# -*- coding: utf-8 -*-
"""qibla

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1UK-U7P-Ab71HAPQJe0c4NNwdZ0TKaGxG
"""

import math

from dataclasses import dataclass

@dataclass(frozen=True)
class QiblaDirection:
    decimal: float  # decimal degrees, 0..360
    degree: str

def direction (lat : float, long: float) -> QiblaDirection:


  φ_Location = lat
  λ_Location = long
  φ_Kaabah = 21.4225
  λ_Kaabah = 39.8262
  Difference_Longitude   = abs(λ_Location-λ_Kaabah )


  #Calculation of Qibla Direction
  import math

  A = math.sin(math.radians(abs(Difference_Longitude)))
  B = math.cos(math.radians(φ_Location))*math.tan(math.radians(φ_Kaabah))
  C = math.sin(math.radians(φ_Location))  * math.cos(math.radians(Difference_Longitude))
  D = A/(B-C)
  θ     = math.degrees(math.atan(D))

  #Determine the Azimuth of the Qibla
  if Difference_Longitude > 180:
      delta_λ = 360 - Difference_Longitude
  else:
      delta_λ = Difference_Longitude

  if θ > 0:
      if λ_Location > λ_Kaabah:
          quadrant = "UB"  # Utara Barat
      elif λ_Location <= λ_Kaabah:
          quadrant = "UT"  # Utara Timur
      elif λ_Location < 0:
          if c >= 180:
              quadrant = "UB"
          else:
              quadrant = "UT"
  elif θ < 0:
      if λ_Location > λ_Kaabah:
          quadrant = "SB"  # Selatan Barat
      elif λ_Location <= λ_Kaabah:
          quadrant = "ST"  # Selatan Timur
      elif λ_Location < 0:
          if c >= 180:
              quadrant = "SB"
          else:
              quadrant = "ST"

  if quadrant == "UB":
      azimuth_kiblat = 360 - θ
  elif quadrant == "SB":
      azimuth_kiblat = 180 - θ
  elif quadrant == "UT":
      azimuth_kiblat = θ
  elif quadrant == "ST":
      azimuth_kiblat = 180 + θ

  # To Convert in Degree Form

  degrees = int(azimuth_kiblat)
  decimal_part = azimuth_kiblat - degrees
  minutes_total = decimal_part = 60
  minutes = int(minutes_total)
  seconds = round((minutes_total - minutes) * 60)
  #print(f'The azimuth of the Qibla for Location with coordinate {φ_Location} Latitude, {λ_Location} Longitude, is {degrees}° {minutes}′ {seconds}″')
  outputdegree = f"{degrees}° {minutes}′ {seconds}″"

  return QiblaDirection(decimal=azimuth_kiblat, degree=outputdegree)




