from django.shortcuts import render, redirect
# from django.views.generic import ListView, DetailView,  DeleteView, UpdateView, CreateView
from django.views import View, generic
from django.db import connection as conn


# Create your views here.
class Dashboard(generic.TemplateView):
    template_name = "layout/layout.html"
    # @method_decorator(login_required(login_url='maps:login'))
        
    def get(self, request):
        with conn.cursor() as all_:
            all_.execute("SELECT * from global_oku")
            all = all_.fetchall()
        
        with conn.cursor() as kel:
            kel.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (kode_desa, desa, kecamatan, kab_kota, provinsi, jumlah_pen, jumlah_kk, luas_desa),'penduduk', (pria, wanita, belum_kawin, kawin, cerai_hidup, cerai_mati, wajib_ktp, islam, kristen, khatolik, hindu, budha, konghucu, kepercayaan_lain, u0, u5, u10, u15, u20, u25, u30, u35, u40, u45, u50, u55, u60, u65, u70, u75),'pekerjaan_pendidikan', (tidak_sekolah, belum_tamat, tamat_sd, sltp, slta, diploma_i, diploma_ii, diploma_iv, strata_ii, strata_iii, tidak_bekerja, aparatur_pemerintah, tenaga_pendidik, wiraswasta, pertanian, tenaga_kesehatan, pensiunan, pegawai, tentara, kepolisian, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, dosen, guru, pilot, pengacara, notaris, arsitek, akuntan, konsultan, dokter, bidan, perawat, psikiater, sopir, lainnya),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kelurahan")
            kel_res_ = kel.fetchall()
        
        with conn.cursor() as kec:
            kec.execute("SELECT kecamatan, json_build_object('type', 'Feature', 'administrasi', (id, kecamatan),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kecamatan")
            kec_res_ = kec.fetchall()
        
        with conn.cursor() as knt:
            knt.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark),'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_kantor_administrasi a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            knt_res = knt.fetchall()
        
        kec_res = []
        for i in kec_res_:
            kec_res.append(i[1])
        
        kel_res = []
        for i in kel_res_:
            kel_res.append(i[0])
        
        kntl = []
        for i in knt_res:
            kntl.append(i[0])
        
        with conn.cursor() as kantor_administrasi:
            kantor_administrasi.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark),'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_kantor_administrasi a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            kantor_administrasi_res = kantor_administrasi.fetchall()
            
        kantor_administrasi_result = []
        for i in kantor_administrasi_res:
            kantor_administrasi_result.append(i[0])
            
        with conn.cursor() as layanan_kesehatan:
            layanan_kesehatan.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_layanan_kesehatan a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            layanan_kesehatan_res = layanan_kesehatan.fetchall()
        
        layanan_kesehatan_result = []
        for i in layanan_kesehatan_res:
            layanan_kesehatan_result.append(i[0])
            
        with conn.cursor() as puskesmas:
            puskesmas.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_puskesmas a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            puskesmas_res = puskesmas.fetchall()
        
        puskesmas_result = []
        for i in puskesmas_res:
            puskesmas_result.append(i[0])
            
        with conn.cursor() as rumah_sakit:
            rumah_sakit.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_rumah_sakit a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            rumah_sakit_res = rumah_sakit.fetchall()
        
        rumah_sakit_result = []
        for i in rumah_sakit_res:
            rumah_sakit_result.append(i[0])
            
        with conn.cursor() as sarana_ibadah:
            sarana_ibadah.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_sarana_ibadah a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            sarana_ibadah_res = sarana_ibadah.fetchall()
        
        sarana_ibadah_result = []
        for i in sarana_ibadah_res:
            if i[0]["administrasi"]['f1'] == None:
                i[0]["administrasi"]['f1'] = "None"
            sarana_ibadah_result.append(i[0])
            
        with conn.cursor() as sarana_pendidikan:
            sarana_pendidikan.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_sarana_pendidikan a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            sarana_pendidikan_res = sarana_pendidikan.fetchall()
        
        sarana_pendidikan_result = []
        for i in sarana_pendidikan_res:
            if i[0]["administrasi"]['f1'] == None:
                i[0]["administrasi"]['f1'] = "None"
            sarana_pendidikan_result.append(i[0])
            
        with conn.cursor() as spbu:
            spbu.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_spbu a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            spbu_res = spbu.fetchall()
        
        spbu_result = []
        for i in spbu_res:
            spbu_result.append(i[0])
            
        with conn.cursor() as stasiun:
            stasiun.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_stasiun a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            stasiun_res = stasiun.fetchall()
        
        stasiun_result = []
        for i in stasiun_res:
            stasiun_result.append(i[0])
            
        with conn.cursor() as terminal:
            terminal.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_terminal_bus a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            terminal_res = terminal.fetchall()
        
        terminal_result = []
        for i in terminal_res:
            terminal_result.append(i[0])
            
        with conn.cursor() as cagar_budaya:
            cagar_budaya.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_cagar_budaya a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            cagar_budaya_res = cagar_budaya.fetchall()
        
        cagar_budaya_result = []
        for i in cagar_budaya_res:
            cagar_budaya_result.append(i[0])
            
        with conn.cursor() as gardu_listrik:
            gardu_listrik.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (a.namobj, a.remark), 'geometry', ST_AsGeoJSON(a.geom :: geometry) :: json) from geo_gardu_listrik a join geo_prov_kab b on st_intersects(a.geom,b.geom)")
            gardu_listrik_res = gardu_listrik.fetchall()
        
        gardu_listrik_result = []
        for i in gardu_listrik_res:
            gardu_listrik_result.append(i[0])
        
        print(all)
        context = {
        'title':'Dashboard',
        'kelurahan':kel_res,
        'kecamatan':kec_res,
        'kantor_administrasi':kantor_administrasi_result,
        'layanan_kesehatan':layanan_kesehatan_result,
        'puskesmas':puskesmas_result,
        'rumah_sakit':rumah_sakit_result,
        'sarana_ibadah':sarana_ibadah_result,
        'sarana_pendidikan':sarana_pendidikan_result,
        'spbu':spbu_result,
        'stasiun':stasiun_result,
        'terminal':terminal_result,
        'cagar_budaya':cagar_budaya_result,
        'gardu_listrik':gardu_listrik_result,
        'global':all[0],
        'kntl':kntl
        }
        return render (request, self.template_name, context)
    
class SearchLocation(generic.TemplateView):
    template_name = "maps/maps.html"
    
    # @method_decorator(login_required(login_url='maps:login'))
    def get(self, request):
        return redirect("apps:dashboard")
        
    def post(self, request):        

                
        return render (request, self.template_name, {"title":"NA - CheckByPolygon"})#,"polbang":polbang,"poltrees":poltrees,'jumlah_bangunan' : len(polbang)+len(poltrees), 'batas_co':batas_desa, 'search_by': 'address', 'address':f'{self.search_desa},{self.search_kab}', 'poly_desa':poly_desa, 'lat':lat, 'lng':lng, 'zoom':zoom, 'line_jalan':line_jalan})
    
class SearchRadius(generic.TemplateView):
    template_name = "maps/maps.html"
    
    # @method_decorator(login_required(login_url='maps:login'))
    def get(self, request):
        return redirect("apps:dashboard")
        
    def post(self, request):        

        return render (request, self.template_name, {"title":"NA - CheckByRadius"})#, "polbang":polbang, "poltrees":poltrees, 'jumlah_bangunan' : len(polbang)+len(poltrees), 'batas_co':zon, 'search_by': 'radius', 'address':'None'})

import re
class SearchPolygon(generic.TemplateView):
    template_name = "maps/maps.html"

    # @method_decorator(login_required(login_url='maps:login'))
    def get(self, request):
        return redirect("apps:dashboard")
        
    def post(self, request):        
        
        return render (request, self.template_name, {"title":"NA - CheckByPolygon"})#,"polbang":polbang,"poltrees":poltrees,'jumlah_bangunan' : len(polbang)+len(poltrees), 'batas_co':zon, 'search_by': 'polygon', 'address':'None', 'lat':lat, 'lng':lng, 'zoom':zoom, 'line_jalan':line_jalan})

