from oneclick.analysis.analysis import Analysis
from subprocess import Popen,PIPE
from oneclick.configTest import Config,Status
from cast_common.hlRestCall import HLRestCall
from cast_common.util import run_process,create_folder,check_process
from os.path import abspath

import sys

class HLAnalysis(Analysis):
    @property
    def name(self) -> str:
        return f'Highlight Analysis'
    def choose(self) -> bool:
        return True

    def __init__(cls):
        pass

    def run(cls):
        config = cls.config
        
        rest = HLRestCall(hl_base_url=config.hl_url,hl_user=config.hl_user,hl_pswd=config.hl_password,hl_instance=config.hl_instance)

        try:
            process = {}
            for appl in config.applist:
                app_name = appl['name']

                hl_status = appl['status']['highlight']=Status.ANALYSIS_START


                if hl_status == '' or hl_status.startswith('Error'):

                    # cls.log.info(f'Running Highlight analysis for {config.project_name}\{appl}')
                    cls.log.info(f'Running Highlight analysis for {appl}')
                    app_id = rest.get_app_id(appl)
                    if app_id is None:
                        cls.log.info(f'Application {appl} not found in Highlight, Creating Application {appl}...')
                        resp_code = rest.create_an_app(config.hl_instance, appl)
                        if resp_code == 200:
                            cls.log.info(f'Application {appl} created inside Highlight.')
                            app_id = rest.get_app_id(appl)
                        else:
                            cls.log.info(f'Not able to Application {appl} inside Highlight due to some error!')                       

                    hl_sourceDir = abspath(f'{config.work}\\{config.project_name}\\{appl}')
                    hl_workingDir = abspath(f'{config.oneclick_work}\\{config.project_name}\\HL_ANALYSIS_RESULT\\{appl}')
                    create_folder(f'{config.oneclick_work}\\{config.project_name}\\HL_ANALYSIS_RESULT')
                    create_folder(hl_workingDir)
                    java_home = config.java_home
                    if len(java_home) > 0:
                        java_home = f'{java_home}'

                    # args = [abspath(f'{config.java_home}/bin/java.exe'),
                    #         '-jar',config.hl_cli,
                    #         '--sourceDir', hl_sourceDir,
                    #         '--workingDir' , hl_workingDir,
                    #         '--companyId', str(config.hl_instance),
                    #         '--analyzerDir',config.analyzer_dir,
                    #         '--perlInstallDir',config.perl_install_dir,
                    #         '--applicationId', str(app_id),
                    #         '--serverUrl', config.hl_url.rsplit('/',1)[0],
                    #         '--login',config.hl_user,
                    #         '--password',config.hl_password]

                    args = [abspath(f'{config.base}/scripts/runHighlight.bat'),
                            abspath(f'{config.java_home}/bin/java.exe'),
                            config.hl_cli,
                            hl_sourceDir,
                            hl_workingDir,
                            str(config.hl_instance),
                            config.analyzer_dir,
                            config.perl_install_dir,
                            str(app_id),
                            config.hl_url.rsplit('/',1)[0],
                            config.hl_user,
                            config.hl_password]
                    try:
                        proc = run_process(args,wait=False)
                        pass

                    except FileNotFoundError as e:
                        cls.log.error(f'Unable to launch analysis process {e}')
                        cls.log.error(args)
                        return e.errno
                else:
                    proc = None

                cls.track_process(proc,"Highlight",appl)
            return 0


            # error = False
            # for appl in process:
            #     cls.log.info(f'Tracking Highlight analysis for {config.project_name}\{appl}')
            #     status,output = check_process(process[appl])
            #     if status != 0:
            #         cls.log.error(f'Error analyzing {appl}')
            #         error = True
            # if error:
            #     # TODO: add more desriptive error message
            #     raise RuntimeError ("")
        except KeyError as e:
            msg = str(e)
            if 'application not found:' in msg:
                cls.log.error(msg)
                sys.exit(1)