#!/usr/bin/python
# -*- coding: utf-8 -*-

# status_deps.py file is part of slpkg.

# Copyright 2014-2015 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
# All rights reserved.

# Slpkg is a user-friendly package manager for Slackware installations

# https://github.com/dslackw/slpkg

# Slpkg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import os
import sys

from slpkg.utils import Utils
from slpkg.messages import Msg
from slpkg.graph import Graph
from slpkg.splitting import split_package
from slpkg.__metadata__ import MetaData as _meta_

from slpkg.pkg.find import find_package


class DependenciesStatus(object):
    """Print dependencies status used by packages
    """
    def __init__(self, image):
        self.image = image
        self.meta = _meta_
        self.msg = Msg()
        self.grey = self.meta.color["GREY"]
        self.green = self.meta.color["GREEN"]
        self.endc = self.meta.color["ENDC"]
        self.dmap = {}
        self.count_pkg = 0
        self.count_dep = 0
        self.dep_path = self.meta.log_path + "dep/"
        self.logs = find_package("", self.dep_path)
        if not self.logs:
            self.no_logs()
        self.installed = find_package("", self.meta.pkg_path)

    def data(self):
        """Check all installed packages and create
        dictionary database
        """
        for pkg in self.installed:
            if os.path.isfile(self.meta.pkg_path + pkg):
                name = split_package(pkg)[0]
                for log in self.logs:
                    deps = Utils().read_file(self.dep_path + log)
                    for dep in deps.splitlines():
                        if name == dep:
                            if name not in self.dmap.keys():
                                self.dmap[name] = [log]
                                self.count_dep += 1
                            else:
                                self.dmap[name] += [log]
                                self.count_pkg += 1

    def show(self):
        """Show dependencies status
        """
        self.data()
        print("")
        self.msg.template(78)
        print("| {0}{1}{2}".format("Dependencies", " " * 20, "Packages"))
        self.msg.template(78)
        for key, value in self.dmap.iteritems():
            print("  {0}{1}{2}{3}{4}".format(
                self.green, key, self.endc, " " * (32-len(key)),
                ", ".join(value)))
        self.summary()
        if self.image:
            Graph(self.image).dependencies(self.dmap)

    def tree(self):
        """Like tree view mode
        """
        self.msg.template(78)
        print("| Dependencies\n"
              "|   Packages")
        self.msg.template(78)
        self.data()
        for pkg, dep in self.dmap.iteritems():
            print("+ {0}{1}{2}".format(self.green, pkg, self.endc))
            print("|")
            for d in dep:
                print("+-- {0}".format(d))
                print("|")
            sys.stdout.write("\x1b[1A{0}\n".format(" "))
            sys.stdout.flush()
        self.summary()

    def no_logs(self):
        """Print message if no logs found
        """
        print("\n  There were no logs files. Obviously not used the\n"
              "  installation method with the command:\n"
              "  '$ slpkg -s <repository> <packages>' yet.\n")
        raise SystemExit()

    def summary(self):
        """Summary by packages and dependencies
        """
        print("\nStatus summary")
        print("=" * 79)
        print("{0}found {1} dependencies in {2} packages.{3}\n".format(
            self.grey, self.count_dep, self.count_pkg, self.endc))
