# encoding:utf-8

from .order import Order


class Trade(object):
    """
    Basic order class.

    Attributes
    ----------
    task_id : str
        id of the task.
    entrust_no : str
        ID of the order.
    entrust_action : str
    symbol : str
    fill_price : float
    fill_size : int
    fill_date : int
    fill_time : int
    fill_no : str
        ID of this trade.

    Methods
    -------


    """
    
    def __init__(self, order=None):
        self.task_id = 0
        self.entrust_no = ""
        
        self.entrust_action = ""
        
        self.symbol = ""

        self.fill_no = ""
        self.fill_price = 0.0
        self.fill_size = 0
        self.fill_date = 0
        self.fill_time = 0
        
        self.commission = 0.0

        if order is not None:
            if isinstance(order, Order):
                self.init_from_order(order)
            else:
                raise ValueError("init_from_order only accept argument of type Order.")
        
    def init_from_order(self, order):
        self.task_id = order.task_id
        self.entrust_no = order.entrust_no
        self.symbol = order.symbol
        self.entrust_action = order.entrust_action
    
    def set_fill_info(self, price, size, date, time, no):
        self.fill_price = price
        self.fill_size = size
        self.fill_date = date
        self.fill_time = time
        self.fill_no = no
        
    @classmethod
    def create_from_dict(cls, dic):
        trade_ind = cls()
        trade_ind.__dict__.update(dic)
        return trade_ind
    
    def __repr__(self):
        return "{0.fill_date:8d}({0.fill_time:8d}) " \
               "{0.entrust_action:6s} {0.symbol:10s}@{0.fill_price:.3f}" \
               "  size = {0.fill_size}".format(self)
    
    def __str__(self):
        return self.__repr__()


class TaskInd(object):
    def __init__(self, task_id=0, task_status="", task_algo="", task_msg=""):
        self.task_id = task_id
        self.task_status = task_status
        self.task_algo = task_algo
        self.task_msg = task_msg

    @classmethod
    def create_from_dict(cls, dic):
        rsp = cls()
        rsp.__dict__.update(dic)
        return rsp
    
    def __repr__(self):
        return "task_id = {0.task_id:d}  |  task_status = {0.task_status:s}  |  task_algo = {0.task_algo:s}" \
               "\ntask_msg = \n{0.task_msg:s}".format(self)

    def __str__(self):
        return self.__repr__()


class TradeStat(object):
    def __init__(self, symbol=""):
        self.symbol = symbol
        self.buy_filled_size = 0
        self.buy_want_size = 0
        self.sell_filled_size = 0
        self.sell_want_size = 0
    
    def __repr__(self):
        return ("        Want Size      Filled Size  \n"
                "====================================\n"
                "Buy     {0:8.0f}         {1:8.0f}   \n"
                "------------------------------------\n"
                "Sell    {2:8.0f}         {3:8.0f}   \n"
                "".format(self.buy_want_size, self.buy_filled_size,
                          self.sell_want_size, self.sell_filled_size))
    
    def __str__(self):
        return self.__repr__()
