import json
import os

from .svc import svc


# --------------------
## perform the do_post_ver operation
class DoPostVer:
    # --------------------
    ## do_post_ver mainline.
    #
    # @return None
    def run(self):
        svc.log.highlight(f'{svc.gbl.tag}: starting...')

        slug = svc.cfg.do_post_ver.slug
        if slug.lower() in ['none', 'n/a', 'na']:
            svc.log.warn(f'{svc.gbl.tag}: not a web project, skipping (slug: "{slug}")')
            return

        # ensure web-ver is created
        svc.utils_fs.safe_global_dir(svc.cfg.do_post_ver.dst_dir)
        dst_dir = os.path.expanduser(svc.cfg.do_post_ver.dst_dir)

        self._gen_info_json(slug, dst_dir)
        self._save_verification_files(slug, dst_dir)

        svc.log.check(svc.gbl.rc == 0, f'{svc.gbl.tag}: verification files post, rc={svc.gbl.rc}')
        svc.gbl.overallrc += svc.gbl.rc

    # --------------------
    ## create an info.json file to contain various information
    #
    # @param slug     the web page slug
    # @param dst_dir  the destination directory to save the json file
    # @return None
    def _gen_info_json(self, slug, dst_dir):
        info = {
            'slug': slug,
            'version': svc.cfg.version,
            'pypi_url': svc.cfg.do_post_ver.pypi_url,
            'repo_url': svc.cfg.do_post_ver.repo_url,
        }

        info_path = os.path.join(dst_dir, f'{slug}-info.json')
        with open(info_path, 'w', encoding='utf-8', newline='\n') as fp:
            json.dump(info, fp)
        svc.log.line(f'{svc.gbl.tag}: gen info: {info_path}')

    # --------------------
    ## copy verification html files to destination directory
    # assume: skeleton HTML files were generated by medver-pytest
    #
    # @param slug     the web page slug
    # @param dst_dir  the destination directory to save the json file
    # @return None
    def _save_verification_files(self, slug, dst_dir):
        src_dir = os.path.expanduser(svc.cfg.do_post_ver.src_dir)
        self._save_verification_file(slug, dst_dir, 'summary', src_dir)
        self._save_verification_file(slug, dst_dir, 'trace', src_dir)
        self._save_verification_file(slug, dst_dir, 'report', src_dir, src_fname='test_report')

        # TODO any other files go here

    # --------------------
    ## save verification file to destination directory
    # assume: skeleton HTML files were generated by medver-pytest
    #
    # @param slug       the web page slug
    # @param dst_dir    the destination directory to save the html file
    # @param fname      the file to save (no extension)
    # @param src_dir    the source directory (usually out/ver)
    # @param src_fname  the source file name, if different from fname
    # @return None
    def _save_verification_file(self, slug, dst_dir, fname, src_dir, src_fname=None):
        if src_fname is None:
            src_path = os.path.join(src_dir, f'{fname}-min.html')
        else:
            src_path = os.path.join(src_dir, f'{src_fname}-min.html')

        if not os.path.isfile(src_path):
            svc.log.warn(f'{svc.gbl.tag}: src not found, skipping: {src_path}')
            return

        # note extension changed to html
        dst_fname = f'{slug}-{fname}.html'
        svc.utils_fs.safe_copy_file(src_path, dst_dir, dst_fname)
        svc.log.line(f'{svc.gbl.tag}: saved: {os.path.join(dst_dir, dst_fname)}')
