import os

import geojson
import numpy as np
import pandas as pd
from django.contrib.gis.geos import GEOSGeometry
from geojson import MultiPolygon

from service import models
import requests
from tqdm import tqdm
from django.core.cache import cache
from django.conf import settings
from rest_framework.response import Response
from rest_framework.viewsets import ReadOnlyModelViewSet

import psycopg2
from postamates.settings import DB_URL


def run_psql_command():
    connection = psycopg2.connect(
        DB_URL
    )
    try:
        cursor = connection.cursor()
        command = "CALL public.pivot_dist();"
        cursor.execute(command)
        connection.commit()
    except psycopg2.Error as e:
        print("Error executing command:", e)
    finally:
        cursor.close()
        connection.close()


def load_ao_and_rayons(
        ao_filepath: str,
        rayons_filepath: str,
):
    models.AO.objects.all().delete()
    models.Rayon.objects.all().delete()
    gj = geojson.load(ao_filepath)
    objs = gj['features']
    for obj in tqdm(objs, desc='Loading AOs...'):
        name = obj['properties']['okrug']
        coords = obj['geometry']['coordinates']
        models.AO.objects.create(**{'name': name, 'polygon': GEOSGeometry(str(MultiPolygon(coords)))})
    gj = geojson.load(rayons_filepath)
    objs = gj['features']
    for obj in tqdm(objs, desc='Loading Rayons...'):
        name = obj['properties']['rayon']
        coords = obj['geometry']['coordinates']
        okr = obj['properties']['okrug']
        ao = models.AO.objects.get(name=okr)
        models.Rayon.objects.create(**{'name': name, 'polygon': GEOSGeometry(str(MultiPolygon(coords))), 'AO': ao})


def load_dist(filepath: str):
    models.PointDist.objects.all().delete()
    df = pd.read_csv(filepath)
    for row in df.to_dict('records'):
        row['id1'] = models.PlacementPoint.objects.get(pk=row.get('id1'))
        row['id2'] = models.PlacementPoint.objects.get(pk=row.get('id2'))
        models.PointDist.objects.create(**row)


def log_to_telegram(msg):
    requests.post('https://api.telegram.org/bot6275517704:AAHVp_qv9d9NU740JJdOM2fJdgS4r1AgJrw/sendMessage',
                  json={"chat_id": "-555238820", "text": str(settings.DOMAIN)+'\n'+msg})


def cached_func(key, func, timeout=settings.CACHE_TIMEOUT, *args, **kwargs):
    d = cache.get(key)
    if d is None:
        d = func(*args, **kwargs)
        cache.set(key, d, timeout)
    return d


class CustomReadOnlyModelViewSet(ReadOnlyModelViewSet):
    def list(self, request, *args, **kwargs):
        def f():
            return ReadOnlyModelViewSet.list(self, request, *args, **kwargs).data

        d = cached_func(self.__class__.__name__, f)
        return Response(d)


def create_columns_dist(row):
    return pd.Series(row['min_distance_to_group'])
