#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
#
# Distributed under terms of the MIT license.

"""
Strategy base class
"""

from abc import ABCMeta, abstractmethod
from tinydb import TinyDB, Query
from node import Node
import json


class Strategy(object):
    def __init__(self, this_controller, this_description=None):
        self.description = this_description
        self.controller = this_controller
        self.ledger = TinyDB("ledger.json")
        self.db = TinyDB("nodes.json")
        self.nodes = []

    @abstractmethod
    def store_file(self, file_bytes, file_name):
        pass

    @abstractmethod
    def retrieve_file(self, file_name, locations):
        pass

    @abstractmethod
    def get_time(self):
        pass

    def getNodes(self):
        self.nodes = []
        for item in self.db:
            node = Node(item['mac'],item['ip'],item['port'],item['units'])
            self.nodes.append(node)
        return self.nodes

    def getNodesWithFile(self,filename):
        macs = self.ledger.search(Query().file_name == filename)
        self.nodes = []
        for item in macs:
            mac = item["location"]
            dbnode = self.db.get(Query().mac == mac)
            if(dbnode == None):
                continue
            node = Node(dbnode['mac'],dbnode['ip'],dbnode['port'],dbnode['units'])
            self.nodes.append(node)
        return self.nodes

    def getFileSize(self, filename):
        file = self.ledger.get(Query().file_name == filename)
        return file['size']

[((22, 22, 22, 43), 'tinydb.TinyDB', 'TinyDB', ({(22, 29, 22, 42): '"""ledger.json"""'}, {}), "('ledger.json')", False, 'from tinydb import TinyDB, Query\n'), ((23, 18, 23, 38), 'tinydb.TinyDB', 'TinyDB', ({(23, 25, 23, 37): '"""nodes.json"""'}, {}), "('nodes.json')", False, 'from tinydb import TinyDB, Query\n'), ((41, 19, 41, 74), 'node.Node', 'Node', ({(41, 24, 41, 35): "item['mac']", (41, 36, 41, 46): "item['ip']", (41, 47, 41, 59): "item['port']", (41, 60, 41, 73): "item['units']"}, {}), "(item['mac'], item['ip'], item['port'], item['units'])", False, 'from node import Node\n'), ((53, 19, 53, 82), 'node.Node', 'Node', ({(53, 24, 53, 37): "dbnode['mac']", (53, 38, 53, 50): "dbnode['ip']", (53, 51, 53, 65): "dbnode['port']", (53, 66, 53, 81): "dbnode['units']"}, {}), "(dbnode['mac'], dbnode['ip'], dbnode['port'], dbnode['units'])", False, 'from node import Node\n'), ((46, 34, 46, 41), 'tinydb.Query', 'Query', ({}, {}), '()', False, 'from tinydb import TinyDB, Query\n'), ((58, 31, 58, 38), 'tinydb.Query', 'Query', ({}, {}), '()', False, 'from tinydb import TinyDB, Query\n'), ((50, 33, 50, 40), 'tinydb.Query', 'Query', ({}, {}), '()', False, 'from tinydb import TinyDB, Query\n')]