import os
import mechanize
import sys, logging
import urllib
import urllib.request
import urllib.error
#from urllib import request, error
import json

#import poster
#from poster.encode import multipart_encode
#from poster.streaminghttp import register_openers

class DeleteRequest(mechanize.Request):
    "Extend the mechanize Request class to allow a http DELETE"
    def get_method(self):
        return "DELETE"
        
class PutRequest(mechanize.Request):
    "Extend the mechanize Request class to allow a http PUT"
    def get_method(self):
        return "PUT"

class Response:
    def __init__(self, status_code, content):
        self.status_code = status_code
        self.content = content
        
class request_overide:
    
    logger = logging.getLogger("mechanize")
    logger.addHandler(logging.StreamHandler(sys.stdout))
    logger.setLevel(logging.DEBUG)
    Browser = mechanize.Browser()
    
    def get(url:str, *args, **kwargs):
        data = kwargs.get('data', None)
        headers = kwargs.get('headers', None)
        
        try:
            request = mechanize.Request(url,data=data,headers=headers)
            self.Browser.method = 'GET'
            response = Browser.open(request)
            status_code = response.code
            content = response.read()
            
            return Response(status_code, content)
            
        except Exception as e:
            print(f"Error Connecting to server: {e}")
            print("Have you started the server ?")
            return False
                
    def post(url:str, *args, **kwargs):
        data = kwargs.get('data', None)
        headers = kwargs.get('headers', None)
        
        Browser = mechanize.Browser()
        request = mechanize.Request(url,data=data,headers=headers)
        response = Browser.open(request)
        status_code = response.code
        content = response.read()
        
        return Response(status_code, content)

    def delete(url:str, *args, **kwargs):
        data = kwargs.get('data', None)
        headers = kwargs.get('headers', None)
        
        #Browser = mechanize.Browser()
        #Browser.method = 'DELETE'
        #request = DeleteRequest(url,data=data,headers=headers)
        
        #response = Browser.open(request)
        #status_code = response.code
        #content = response.read()
        
        status_code = 0
        content = ""
        return Response(status_code, content)
        
    def upload(url:str, *args, **kwargs):
        data = kwargs.get('data', None)
        headers = kwargs.get('headers', None)
        files = kwargs.get('files', None)
        filename = kwargs.get('filename', None)
        
        # add the content length to the headers
        #headers["Content-Length"] = os.stat(filename).st_size
        #headers["Content-Type"] = "multipart/form-data"
        
        #print(headers)
        #print(data)
        #print(filename)
        
        # Register the streaming http handlers with urllib
        #register_openers()
        
        #with open(filename,'rb') as f:
        #params = {"file": b"{f}", "addToWorkspaces": data['addToWorkspaces']}
        #datagen, fheaders = multipart_encode(params)
        
        
        #print(fheaders)
        #print(datagen)
        
        #data['file'] = files['file']
        
        
        
        #data = json.dumps(data).encode("utf-8")
        
        #Browser = mechanize.Browser()
        #uprequest = urllib.request.Request(url,data=datagen,headers=fheaders)
        #response = urllib.request.urlopen(uprequest)
            
        #request = urllib2.Request('http://localhost', open(filename, 'rb'), headers=headers)
        
        
        #response = Browser.open(request)
        #response = mechanize.urlopen(uprequest)
        
        #status_code = response.code
        #content = response.read()
        
        
        #json.dumps()
        
        status_code = 0
        content = ""
        return Response(status_code, content)