import requests
from requests.structures import CaseInsensitiveDict
from tqdm import tqdm


class NextCloudDownload:

    @staticmethod
    def download_file(url: str, local_filename: str):
        headers = CaseInsensitiveDict()
        headers["Authorization"] = "Basic a2hhbGVkaWhraEBnbWFpbC5jb206LjlqLmc3VzdTLkJNSlpG"
        with requests.get(url, stream=True, headers=headers) as r:
            r.raise_for_status()
            with open(local_filename, 'wb') as f:
                for chunk in tqdm(r.iter_content(chunk_size=8192), desc=local_filename + " downloading..."):
                    f.write(chunk)
        return local_filename


class MediafireDownload:

    @staticmethod
    def download_file(url: str, local_filename: str):
        with requests.get(url) as r:
            r.raise_for_status()
            with open(local_filename, 'wb') as f:
                for chunk in tqdm(r.iter_content(chunk_size=8192),
                                  desc=local_filename + " downloading from media_fire..."):
                    f.write(chunk)
        return local_filename


class DownloadModel:
    def __init__(self):
        self.nextcloud_ner_url = "https://ds.kplab.ir/remote.php/webdav/models/ner-model.pt?downloadStartSecret=jki8h3a282"
        self.nextcloud_pos_url = "https://ds.kplab.ir/remote.php/webdav/models/pos-model.pt?downloadStartSecret=1kagmql2zy2g"
        self.mediafire_ner_url = "https://download1499.mediafire.com/esxlzswwxfjg/hdrd1ufmrqlns7b/ner-model.pt"
        self.mediafire_pos_url = "https://download946.mediafire.com/jrtqg4twc4ng/raluzq6zjw7f68u/pos-model.pt"

    def download_ner_model(self, name):
        try:
            NextCloudDownload.download_file(self.nextcloud_ner_url, name)
        except:
            MediafireDownload.download_file(self.mediafire_ner_url, name)

    def download_pos_model(self, name):
        try:
            NextCloudDownload.download_file(self.nextcloud_pos_url, name)
        except:
            MediafireDownload.download_file(self.mediafire_pos_url, name)

    # TODO
    def download_oie_model(self, name):
        pass

    # TODO
    def download_nre_model(self, name):
        pass
