# Path: app/py/cuda_addonman/work_remote.py
# def get_url(url, fn, del_first=False):
#     fn_temp = fn+'.download'
#     if os.path.isfile(fn_temp):
#         os.remove(fn_temp)
#     if del_first and os.path.isfile(fn):
#         os.remove(fn)
# 
#     if opt.proxy:
#         proxies = { 'http': opt.proxy, 'https': opt.proxy, }
#     else:
#         proxies = None
#     #print('proxy', proxies)
# 
#     while True:
#         try:
#             r = requests.get(url, proxies=proxies, stream=True)
#             with open(fn_temp, 'wb') as f:
#                 for chunk in r.iter_content(chunk_size=1024):
#                     if chunk: # filter out keep-alive new chunks
#                         f.write(chunk)
#                         #f.flush() commented by recommendation
# 
#             if os.path.isfile(fn_temp):
#                 if os.path.isfile(fn):
#                     os.remove(fn)
#                 os.rename(fn_temp, fn)
#             return
# 
#         except Exception as e:
#             res = app.msg_box(_('Cannot download:\n{}\n{}\n\nRetry?').format(url, str(e)),
#                 app.MB_ABORTRETRYIGNORE + app.MB_ICONWARNING)
#             if res==app.ID_IGNORE: return
#             if res==app.ID_ABORT: return False
#             if res==app.ID_CANCEL: return False


import os
import json
import tempfile
import subprocess
import cudatext_cmd
from datetime import datetime
from cudatext import *
from .work_remote import get_url
from .work_install_helper import after_install
from cudax_lib import get_translation

    do_clone = False
    if not os.path.isdir(dir_plugin):
        res = msg_box_ex(
                _('Addon Manager'),
                _('GitHub repository can be cloned (using "git clone") or can be downloaded as zip file. '+
                ' If you clone, Addon Manager\'s Update dialog will update add-on using "git pull", which is recommended.'),
                [_('Clone repo'), _('Download as zip'), _('Cancel')],
                MB_ICONQUESTION)
        if res==2 or res==None:
            return
        if res==0:
            do_clone = True

    if do_clone:
        try:
            subprocess.call(['git', 'clone', url], cwd=dir_py)
        except:
            msg_box(_('Error running Git command'), MB_OK+MB_ICONERROR)
            return

        if os.path.isdir(dir_plugin):
            after_install(module)
            #rescan_plugins()
            msg_box(_('Repo was cloned.\nRestart CudaText to make this plugin visible.'), MB_OK+MB_ICONINFO)
        else:
            msg_box(_('Could not clone the repo'), MB_OK+MB_ICONERROR)

        return


# Path: app/py/cuda_addonman/work_remote.py
# def get_url(url, fn, del_first=False):
#     fn_temp = fn+'.download'
#     if os.path.isfile(fn_temp):
#         os.remove(fn_temp)
#     if del_first and os.path.isfile(fn):
#         os.remove(fn)
# 
#     if opt.proxy:
#         proxies = { 'http': opt.proxy, 'https': opt.proxy, }
#     else:
#         proxies = None
#     #print('proxy', proxies)
# 
#     while True:
#         try:
#             r = requests.get(url, proxies=proxies, stream=True)
#             with open(fn_temp, 'wb') as f:
#                 for chunk in r.iter_content(chunk_size=1024):
#                     if chunk: # filter out keep-alive new chunks
#                         f.write(chunk)
#                         #f.flush() commented by recommendation
# 
#             if os.path.isfile(fn_temp):
#                 if os.path.isfile(fn):
#                     os.remove(fn)
#                 os.rename(fn_temp, fn)
#             return
# 
#         except Exception as e:
#             res = app.msg_box(_('Cannot download:\n{}\n{}\n\nRetry?').format(url, str(e)),
#                 app.MB_ABORTRETRYIGNORE + app.MB_ICONWARNING)
#             if res==app.ID_IGNORE: return
#             if res==app.ID_ABORT: return False
#             if res==app.ID_CANCEL: return False


# Path: app/py/cuda_addonman/work_github.py
import os
import json
import tempfile
import subprocess
import cudatext_cmd
from datetime import datetime
from cudatext import *
from .work_remote import get_url
from .work_install_helper import after_install
from cudax_lib import get_translation


    do_clone = False
    if not os.path.isdir(dir_plugin):
        res = msg_box_ex(
                _('Addon Manager'),
                _('GitHub repository can be cloned (using "git clone") or can be downloaded as zip file. '+
                ' If you clone, Addon Manager\'s Update dialog will update add-on using "git pull", which is recommended.'),
                [_('Clone repo'), _('Download as zip'), _('Cancel')],
                MB_ICONQUESTION)
        if res==2 or res==None:
            return
        if res==0:
            do_clone = True

    if do_clone:
        try:
            subprocess.call(['git', 'clone', url], cwd=dir_py)
        except:
            msg_box(_('Error running Git command'), MB_OK+MB_ICONERROR)
            return

        if os.path.isdir(dir_plugin):
            after_install(module)
            #rescan_plugins()
            msg_box(_('Repo was cloned.\nRestart CudaText to make this plugin visible.'), MB_OK+MB_ICONINFO)
        else:
            msg_box(_('Could not clone the repo'), MB_OK+MB_ICONERROR)

        return


    get_url(url+'/zipball/'+branch, fn, True)