from django.utils.safestring import mark_safe
from corehq.apps.indicators.admin import BaseIndicatorAdminInterface
from corehq.apps.indicators.admin.forms import (CouchIndicatorForm, CountUniqueCouchIndicatorForm,
                                                MedianCouchIndicatorForm, SumLastEmittedCouchIndicatorForm)
from corehq.apps.indicators.models import (CouchIndicatorDef, CountUniqueCouchIndicatorDef, MedianCouchIndicatorDef,
                                           SumLastEmittedCouchIndicatorDef)
from corehq.apps.reports.datatables import DataTablesColumn


class CouchIndicatorAdminInterface(BaseIndicatorAdminInterface):
    name = CouchIndicatorDef.get_nice_name()
    slug = "couch_simple"
    document_class = CouchIndicatorDef
    form_class = CouchIndicatorForm

    description = "Returns the reduced value of an indicator couch view."
    detailed_description = mark_safe("""<p>These indicators are generated by returning the reduced
    value of a couch view by using the <code>indicator_key</code> specified.</p>
    <p>This couch view emits in the following two formats:
    <ul><li>["all", "<code>project</code>, "<code>indicator_key</code>", <code>year</code>,
    <code>month</code>, <code>day</code>]</li>
    <li>["user", "<code>project</code>, "<code>user_id</code>", "<code>indicator_key</code>", <code>year</code>,
    <code>month</code>, <code>day</code>]</li></ul></p>""")

    @property
    def headers(self):
        header = super(CouchIndicatorAdminInterface, self).headers
        header.insert_column(DataTablesColumn("Title"), -3)
        header.insert_column(DataTablesColumn("Description"), -3)
        header.insert_column(DataTablesColumn("Couch View"), -3)
        header.insert_column(DataTablesColumn("Indicator Key"), -3)
        header.insert_column(DataTablesColumn("Time Shifts"), -3)
        return header


class CountUniqueCouchIndicatorAdminInterface(CouchIndicatorAdminInterface):
    name = CountUniqueCouchIndicatorDef.get_nice_name()
    description = "Emits the unique count of emitted values from an indicator couch view."
    slug = "couch_count_unique"
    document_class = CountUniqueCouchIndicatorDef
    form_class = CountUniqueCouchIndicatorForm


class MedianCouchIndicatorAdminInterface(CouchIndicatorAdminInterface):
    name = MedianCouchIndicatorDef.get_nice_name()
    description = "Emits the median emitted values from an indicator couch view."
    slug = "couch_median"
    document_class = MedianCouchIndicatorDef
    form_class = MedianCouchIndicatorForm


class SumLastEmittedCouchIndicatorAdminInterface(CouchIndicatorAdminInterface):
    name = SumLastEmittedCouchIndicatorDef.get_nice_name()
    description = "Sums the last emitted value of a unique id."
    slug = "couch_sum_last_unique"
    document_class = SumLastEmittedCouchIndicatorDef
    form_class = SumLastEmittedCouchIndicatorForm
