# 2. could newer data have come already?
# 3. [+] let's parse the new data 
# 4. [-] commit to repo
# 5. upload to database
# 6. create a PDF handout or other assets



# plotting

import matplotlib.pyplot as plt

from access import get_dataframe


dfa, dfq, dfm = (get_dataframe(freq) for freq in 'aqm')

start_year = 2016

renamer = {'INDPRO_yoy': ('Промышленное производство', 'темп прироста за 12 мес., %'),
           'GDP_yoy': ('Валовый внутренний продукт', 'темп прироста за 12 мес., %'),
           'RETAIL_SALES_yoy': ('Розничные продажи', 'темп прироста за 12 мес., %'),
           'CPI_rog': ('Индекс потребительских цен', 'к пред.периоду, %'),
           'WAGE_REAL_rog':('Реальная заработная плата', 'к пред.периоду, %'),
           'AGROPROD_yoy':('Сельскохозяйственное производство', 'темп прироста за 12 мес., %')
           }


fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(4*3.5, 3*3.5))    

    ax = df.plot(x_compat=True)
    # set ticks
    ax.set_xticks(minor_ticks, minor=True)
    ax.xaxis.set_ticks(major_ticks, minor=False)
    ax.xaxis.set_major_formatter(dates.DateFormatter('%Y'))
    plt.gcf().autofmt_xdate(rotation=0, ha="center")
    # other formatting
    plt.legend(loc="lower left")
    ax.set_title(title, loc='left', fontdict={'fontsize': 11})
    return ax


def plot_long(df, title, start=2005, end=2020, left_offset=1):
    """Plot starting 2005."""
    df = df[df.index >= str(start)]
    minor_ticks = pd.date_range(str(start - left_offset), str(end), freq='YS')
    major_ticks = pd.date_range(str(start), str(end), freq='5YS')
    return plot(df, title, minor_ticks, major_ticks)


def save(filename):
    plt.savefig(filename)


if __name__ == '_main__':
    import access
    dfa, dfq, dfm = (access.get_dataframe(freq) for freq in 'aqm')
    df = dfm.DWELLINGS_CONSTRUCTION_mln_m2
    ax = plot_long(df, 'Ввод жилья, млн.кв.м')
    plt.show()

def many_plots_per_page(df, nrows, ncols, figsize=A4_SIZE_PORTRAIT, title_font_size=TITLE_FONT_SIZE):
    page_vars = df.columns

    # The following command uses the built-in Pandas mechanism for placing subplots on a page.
    # It automatically increases spacing between subplots and rotates axis ticks if they
    # take up too much space. However, this mechanism is broken in Pandas < 0.17.
    # See: https://github.com/pydata/pandas/issues/11536
    # It also cannot handle multiple variables per subplot, so if we want that, we'll have to
    # replicate parts of the Pandas implementation or write our own.
    axes = df.plot(subplots=True, layout=(nrows, ncols), legend=None, figsize=figsize)

    # Now removing axis labels and adding plot titles.
    for i, axes_row in enumerate(axes):
        for j, ax in enumerate(axes_row):
            var_idx = i * ncols + j
            if var_idx >= len(page_vars):
                # We're at the end of the last page, which is not filled completely.
                break
            ax.set_title(page_vars[var_idx], fontsize=title_font_size)
            format_ax(ax)
    return axes


if __name__== '__main__':
    import access
    dfm = access.get_dataframe('m')    
    save_plots_as_pdf(dfm, 'plt.pdf')