from nose.tools import eq_

from helix.constants.inverter_brand import InverterBrand
from helix.constants.inverter_type import InverterType


def assert_step_is_active(browser, step_number, is_active=True):
    eq_(browser.find_by_css('.navigation_header #step_%d' % step_number).has_class('active'), is_active)


def assert_step_is_completed(browser, step_number, is_completed=True):
    eq_(browser.find_by_css('.navigation_header #step_%d' % step_number).has_class('completed'), is_completed)


def select_inverter_brand(browser, brand=None):
    if brand not in InverterBrand.all():
        raise ValueError('Brand must be a valid InverterBrand')
    browser.select('inverter_brand_id', str(brand))


def submit_inverter_brand_form(browser):
    browser.find_by_css('#inverter_brand_form input[type=submit]').first.click()


def fill_in_power_station(browser, name=None, count=None, run_length=None, inverter_count=None, inverters=()):
    if name is not None:
        browser.fill('power_station_description', str(name))
    if count is not None:
        browser.fill('power_station_quantity', str(count))
    if run_length is not None:
        browser.fill('ac_run_length', str(run_length))
    if inverter_count:
        browser.select('inverter_quantity', str(inverter_count))

    for index, inverter in enumerate(inverters):
        prefix = 'inverter_%d-' % (index + 1)
        if inverter.get('model'):
            browser.select(prefix + 'model', str(inverter['model'].value))
        if inverter.get('strings'):
            browser.select(prefix + 'strings_per_inverter', str(inverter['strings']))
        if inverter.get('sunshade'):
            browser.check(prefix + 'sunshade')
        if inverter.get('dc_switch'):
            browser.check(prefix + 'dc_switch')


def submit_power_station_form(browser):
    browser.find_by_value('Add').first.click()


def show_power_station_form(browser):
    browser.find_by_css('#add_new_power_station').first.click()


def assert_power_station_table_row(browser, row_number, name=None, models=None, strings=None, quantity=None):
    row = browser.find_by_css('.power_station_table tr:nth-child(%d)' % (row_number + 1)).first
    if name:
        eq_(row.find_by_css('td:nth-child(1)').first.text.strip(), name)

    if models and strings:
        eq_(len(row.find_by_css('td:nth-child(2)').find_by_tag('p')), len(models))
        eq_(len(row.find_by_css('td:nth-child(3)').find_by_tag('p')), len(strings))
        for i in range(len(models)):
            eq_(row.find_by_css('td:nth-child(2)').find_by_tag('p')[i].text, models[i])
            eq_(row.find_by_css('td:nth-child(3)').find_by_tag('p')[i].text, strings[i])

    if quantity:
        eq_(row.find_by_css('td:nth-child(4)').first.text, str(quantity))


def edit_power_station(browser, row_number):
    browser.find_by_css('.power_station_table tr:nth-child(%d) i.icon-pencil' % (row_number + 1)).first.click()


def assert_number_of_power_station_table_rows(browser, number_of_rows):
    eq_(len(browser.find_by_css('.power_station_table tr')), number_of_rows + 1)


# Standalone Inverters


def show_standalone_inverter_form(browser):
    browser.find_by_css('#add_standalone_inverter').first.click()


def fill_in_standalone_inverter(browser, quantity=None, model=None, strings=None,
                                sunshade=None, dc_switch=None, ac_run_length=None, splice_box=None):
    if quantity:
        browser.select('inverter-quantity', str(quantity))
    if model:
        browser.select('inverter-model', str(model.value))
    if strings:
        browser.select('inverter-strings_per_inverter', str(strings))
    if sunshade:
        browser.check('inverter-sunshade')
    if dc_switch:
        browser.check('inverter-dc_switch')
    if ac_run_length:
        browser.fill('standalone_ac_run_length', str(ac_run_length))
    if splice_box:
        browser.check('inverter-splice_box')


def submit_standalone_inverter_form(browser):
    browser.find_by_css('#standalone_inverter_form input[type=submit]').first.click()


def edit_standalone_inverter(browser, row_number):
    browser.find_by_css('.standalone_inverter_table tr:nth-child(%d) i.icon-pencil' % (row_number + 1)).first.click()


def assert_number_of_standalone_inverter_table_rows(browser, number_of_rows):
    eq_(len(browser.find_by_css('.standalone_inverter_table tr')), number_of_rows + 1)


def assert_standalone_inverter_row(browser, row_number, model=None, interconnect=None, strings=None, sunshade=None,
                                   dc_switch=None, splice_box=None):
    first_table_row = browser.find_by_css('.standalone_inverter_table tr:nth-child(%d)' % (row_number + 1)).first
    if model is not None:
        eq_(first_table_row.find_by_css('td:nth-child(1)').first.text, model.label)
    if interconnect is not None:
        eq_(first_table_row.find_by_css('td:nth-child(2)').first.text, interconnect)
    if strings is not None:
        eq_(first_table_row.find_by_css('td:nth-child(3)').first.text, str(strings))

    if sunshade is not None:
        eq_(first_table_row.find_by_css('td:nth-child(5) i').has_class('hidden'), not sunshade)
    if dc_switch is not None:
        eq_(first_table_row.find_by_css('td:nth-child(4) i').has_class('hidden'), not dc_switch)
    if splice_box is not None:
        eq_(first_table_row.find_by_css('td:nth-child(3) i').has_class('hidden'), not splice_box)


def assert_standalone_inverter_string_options(browser, expected_list):
    inverter_strings_field_options = browser.find_by_css('#inverter-strings_per_inverter option')
    inverter_strings_field_options_list = list(map(lambda x: x.html, inverter_strings_field_options))
    eq_(len(inverter_strings_field_options_list), len(expected_list))
    eq_(inverter_strings_field_options_list, expected_list)


def show_supervisor_monitor_form(browser):
    browser.find_by_css("#add_supervisor_monitor").first.click()


# PV Supervisor Monitoring


def show_monitoring_form(browser):
    browser.find_by_css('#add_supervisor_monitor').first.click()


def fill_in_monitoring_form(browser, power_source):
    browser.fill('power_source', power_source)


def submit_monitoring_form(browser):
    browser.find_by_css('#supervisor_monitor_form input[type=submit]').first.click()


def edit_supervisor_monitor(browser, row_number):
    browser.find_by_css('.supervisor_monitor_table tr:nth-child(%d) i.icon-pencil' % (row_number + 1)).first.click()


def assert_number_of_supervisor_monitor_table_rows(browser, number_of_rows):
    eq_(len(browser.find_by_css('.supervisor_monitor_table tr')), number_of_rows + 1)


def assert_supervisor_monitor_row(browser, row_number, power_source):
    table_row = browser.find_by_css('.supervisor_monitor_table tr:nth-child(%d)' % (row_number + 1)).first
    eq_(table_row.find_by_css('td:nth-child(1)').first.text, power_source)
