# -*- coding: utf-8 -*-
"""
Created on 2018/10/31

@author: gaoan
"""
import json
import six
import pandas as pd

from tigeropen.common.util.string_utils import get_string
from tigeropen.common.response import TigerResponse

COLUMNS = ['symbol', 'time', 'open', 'high', 'low', 'close', 'volume']
BAR_FIELD_MAPPINGS = {'avgPrice': 'avg_price'}


class QuoteBarResponse(TigerResponse):
    def __init__(self):
        super(QuoteBarResponse, self).__init__()
        self.bars = None
        self._is_success = None

    def parse_response_content(self, response_content):
        response = super(QuoteBarResponse, self).parse_response_content(response_content)
        if 'is_success' in response:
            self._is_success = response['is_success']

        if self.data and isinstance(self.data, list):
            bar_items = []
            for symbol_item in self.data:
                symbol = symbol_item.get('symbol')
                if 'items' in symbol_item:
                    for item in symbol_item['items']:
                        item_values = {'symbol': symbol}
                        for key, value in item.items():
                            if value is None:
                                continue
                            if isinstance(value, six.string_types):
                                value = get_string(value)
                            tag = BAR_FIELD_MAPPINGS[key] if key in BAR_FIELD_MAPPINGS else key
                            item_values[tag] = value
                        bar_items.append([item_values.get(tag) for tag in COLUMNS])

            self.bars = pd.DataFrame(bar_items, columns=COLUMNS)
