from flask import Flask, render_template, request, redirect, url_for, send_from_directory, flash, jsonify, json
from flask import current_app

from datetime import datetime
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user

from model.Utilities import RegEx, ResponseHandler, HtmlHelper, ItemCRUDType
from model.Log import LogData, LogHelper

import os
import config
import re

import mysql.connector
from mysql.connector import Error

from model.ItemCRUD import ItemCRUD
class District:

    isSuccess = False
    resultMessage = ""

    def __init__(self):
        self.isSuccess = False
        self.resultMessage = ""


    def EditDistrict(self, request, district_id):
        district = ItemCRUD(itemType=ItemCRUDType.District)

        district_name = request.form['district_Name'].strip()
        state_id = request.form['state_Id']

        district.EditItem(request=request, childid=district_id, parentid=state_id, childname=district_name,storedprocupdate="UpdateDistrict" )
        self.isSuccess = district.isSuccess
        self.resultMessage = district.resultMessage
        return
            

    def AddDistrict(self, request): 

       district = ItemCRUD(ItemCRUDType.District)

       district_name = request.form['district_Name'].strip()
       state_id = request.form['state_Id']

       district.AddItem(request=request, parentid=state_id, childname=district_name,  storedprocfetch="GetDistrictByNameAndState", storedprocadd="SaveDistrict" )
       self.isSuccess = district.isSuccess
       self.resultMessage = district.resultMessage
       return
 
 

    def GetAllDistricts(self, request):
        district = ItemCRUD(itemType=ItemCRUDType.District)
        districtsdata = district.GetAllData(request=request, storedproc="GetAllDistricts")
        self.isSuccess = district.isSuccess
        self.resultMessage = district.resultMessage
        return districtsdata


    def CheckDistrict(self, request):
        district = ItemCRUD(itemType=ItemCRUDType.District)
        district_name = request.json.get('district_Name', '').strip()
        state_id = request.json.get('state_Id', '')
        result = district.CheckItem(request=request, parentid=state_id, childname=district_name, storedprocfetch="GetDistrictByNameAndState")
        self.isSuccess = district.isSuccess
        self.resultMessage = district.resultMessage
        return result


    # def GetDistrictByID(self, request,district_id):
    #     district = ItemCRUD(itemType=ItemCRUDType.District)
    #     districtdata = district.GetAllData(id=district_id,storedproc="GetDistrictDataByID")
    #     self.isSuccess = district.isSuccess
    #     self.resultMessage = district.resultMessage
    #     return districtdata
    def GetDistrictByID(self, request, district_id):
        district = ItemCRUD(itemType=ItemCRUDType.District)

        districtdata = district.GetDataByID(
            id=district_id,
            storedproc="GetDistrictDataByID"
        )

        if districtdata:
            self.isSuccess = True
        else:
            self.isSuccess = False
            self.resultMessage = "District not found"

        return districtdata
        

#Delete District
    def DeleteDistrict(self, request, district_id):
        district = ItemCRUD(itemType=ItemCRUDType.District)
        district.DeleteItem(request=request,itemID=district_id,storedprocDelete="DeleteDistrict")
        self.isSuccess = district.isSuccess
        self.resultMessage = str(district.resultMessage) 