# ===============================================================================
# Copyright 2016 gabe-parrish
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================

# ============= standard library imports ========================
import os
import os
import pandas as pd
import numpy as np
import datetime
from matplotlib import pyplot as plt

# ============= local library imports ===========================
def intersect(l1, l2):
    intersection = l1 & l2
    return intersection

def formatter(df_dict):
    """

    :param df_dict: a dict of data frames with the keys from '000' to '012', 13 keys total
    :return:
    """

    for key, value in df_dict.iteritems():

        print "key \n {}".format(key)

        good_dates = []
        values = []
        good_dfs = []
        good_vals = []
        for df in value:
            date = df['date'] #pd.to_datetime(df['date'])
            j_30 = df['swc_30cm']
            j_60 = df['swc_60cm']
            j_90 = df['swc_90cm']
            j_110 = df['swc_110cm']
            j_130 = df['swc_130cm']

            vals = (date, j_30, j_60, j_90, j_110, j_130)
            values.append(vals)

        for i in values:
            for date, a, b, c, d, e in zip(i[0], i[1], i[2], i[3], i[4], i[5]):
                if "." not in [a, b, c, d, e]:
                    good_dates.append(date)
                # good_dates.append(date)

        print "good dates {} pixel {}, length {}".format(good_dates, key, len(good_dates))

        # turn good dates into a series to delete the repetitions
        good_dates = set(good_dates)

        # need to test the intersection of all dates with the dates in values
        # for i in values:
        good_dates = intersect(good_dates, set(values[0][0]))

        # gooder_dates = []
        # for i, j in zip(values[0], good_dates):
        #     for day, gday in i, j:
        #         if day not in j:



        print "good dates as a set", good_dates
        print "length of set", len(good_dates)

        print "length of values", len(values)
        for i in values:
            g_dates = []
            g_a = []
            g_b = []
            g_c = []
            g_d = []
            g_e = []
            for date, a, b, c, d, e in zip(i[0], i[1], i[2], i[3], i[4], i[5]):
                if date in good_dates:
                    g_dates.append(date)
                    g_a.append(a)
                    g_b.append(b)
                    g_c.append(c)
                    g_d.append(d)
                    g_e.append(e)

            good_vals.append((g_dates, g_a, g_b, g_c, g_d, g_e))

            print "==="
            print len(g_dates)
            print len(g_a)
            print len(g_b)
            print len(g_c)
            print len(g_d)
            print len(g_e)