from django.urls import reverse
import django_tables2 as tables
from django_tables2.utils import A

from django.utils.html import format_html
from django.utils.safestring import mark_safe

from easy_thumbnails.files import get_thumbnailer

from easy_thumbnails.exceptions import InvalidImageFormatError

from generic.models import CidUser, Examination, Supervisor, findMiddle

from django.contrib.auth.models import User

# Generic selection-enabled table base class.
# Inherit from this class to automatically get a selection checkbox
# column (input name="selection") and table attrs that client-side
# scripts can use to find selectable tables.
class SelectionTable(tables.Table):
    selection = tables.CheckBoxColumn(
        accessor="pk",
        orderable=False,
        attrs={
            'th': {'class': 'selection-col'},
            'td': {'class': 'selection-col'},
            'input': {'name': 'selection'},
        },
        verbose_name=''
    )

    class Meta:
        attrs = {"class": "table js-row-selectable", "data-selection-name": "selection"}

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        try:
            cols = list(self.columns.names())
            if 'selection' in cols:
                cols.remove('selection')
                cols.append('selection')
                try:
                    self.reorder_columns(cols)
                except Exception:
                    self.sequence = tuple(cols)
        except Exception:
            pass

        try:
            token = format(id(self), 'x')
            attrs = getattr(self, 'attrs', None) or {}
            classes = attrs.get('class', '')
            if 'js-row-selectable' not in classes:
                attrs['class'] = (classes + ' js-row-selectable').strip()
            attrs['data-selection-token'] = token
            self.attrs = attrs
        except Exception:
            pass

        self.caption = self.row_selection_controls


    @property
    def row_selection_controls(self):
                token = (self.attrs or {}).get('data-selection-token', format(id(self), 'x'))

                html = (
                        f"<div class=\"d-flex justify-content-between align-items-center mb-2\">"
                        f"  <div>"
                        f"    <button class=\"btn btn-outline-secondary btn-sm\" id=\"toggle-row-selection-{token}\" type=\"button\">Enable row selection</button>"
                        f"    <div id=\"selection-controls-{token}\" class=\"btn-group ms-2\" role=\"group\" style=\"display:none;\">"
                        f"      <button type=\"button\" class=\"btn btn-sm btn-outline-primary\" id=\"select-all-{token}\">Select all</button>"
                        f"      <button type=\"button\" class=\"btn btn-sm btn-outline-secondary\" id=\"clear-selection-{token}\">Clear</button>"
                        f"    </div>"
                        f"  </div>"
                        f"  <div>"
                        f"    <small class=\"text-muted\">Selected: <span id=\"selected-count-{token}\">0</span></small>"
                        f"  </div>"
                        f"</div>"
                )

                script = """
<script>
(function(){
    var token = '__TOKEN__';
    window.__selection_inits = window.__selection_inits || {};
    window.__selection_inits[token] = function(){
        try { console.log && console.log('selection init registered', token); } catch (e) {}
        var toggleBtn = document.getElementById('toggle-row-selection-' + token);
        try { console.log && console.log('selection init running', token, 'table?', !!table); } catch (e) {}
        if (!toggleBtn) return;
        var controlsEl = document.getElementById('selection-controls-' + token);
        var selectAllBtn = document.getElementById('select-all-' + token);
        var clearBtn = document.getElementById('clear-selection-' + token);
        var selectedCountSpan = document.getElementById('selected-count-' + token);
        var table = document.querySelector('table.js-row-selectable[data-selection-token="' + token + '"]');
        if (!table) return;

        var selectionEnabled = false;
        var lastChecked = null;

        function findRowCheckboxes() { var checks = Array.from(table.querySelectorAll('input[type="checkbox"][name="selection"]')); if (!checks.length) checks = Array.from(table.querySelectorAll('input[type="checkbox"]')); return checks; }
        function updateSelectedCount() { if (!selectedCountSpan) return; var checks = findRowCheckboxes(); var count = checks.filter(function(c){ return c.checked; }).length; selectedCountSpan.textContent = count; checks.forEach(function(cb){ var tr = cb.closest('tr'); if (tr) tr.classList.toggle('table-active', cb.checked); }); }
        function setCheckboxesDisabled(disabled) { findRowCheckboxes().forEach(function(cb){ cb.disabled = disabled; }); lastChecked = null; }
        function hideSelectionColumn() { table.querySelectorAll('th, td').forEach(function(cell){ if (cell.querySelector && cell.querySelector('input[type="checkbox"][name="selection"]')) cell.style.display = 'none'; }); }
        function showSelectionColumn() { table.querySelectorAll('th, td').forEach(function(cell){ if (cell.querySelector && cell.querySelector('input[type="checkbox"][name="selection"]')) cell.style.display = ''; }); }

        setCheckboxesDisabled(true); hideSelectionColumn();

        table.addEventListener('click', function(e){
            // Checkbox clicks should always be handled (support shift-range selection),
            // even when row-selection mode is not enabled. Row clicks to toggle a
            // checkbox still require selectionEnabled.
            if (e.target && e.target.matches && e.target.matches('input[type="checkbox"]')){
                var cb = e.target;
                if (e.shiftKey && lastChecked && lastChecked !== cb){
                    var checks = findRowCheckboxes();
                    var start = checks.indexOf(lastChecked);
                    var end = checks.indexOf(cb);
                    if (start > -1 && end > -1){
                        if (start > end){ var tmp = start; start = end; end = tmp; }
                        for (var i = start; i <= end; i++){ if (!checks[i].disabled) checks[i].checked = cb.checked; }
                        updateSelectedCount();
                    }
                }
                lastChecked = cb;
                return;
            }

            if (!selectionEnabled) return;
            var tag = (e.target.tagName || '').toLowerCase();
            if (['a','button','select','textarea','label'].indexOf(tag) !== -1) return;

            var tr = e.target.closest('tr'); if (!tr || !table.contains(tr)) return;
            var cb = tr.querySelector('input[type="checkbox"][name="selection"]') || tr.querySelector('input[type="checkbox"]');
            if (!cb || cb.disabled) return;

            // If the user shift-clicks on a row, perform range selection
            if (e.shiftKey && lastChecked && lastChecked !== cb){
                var checks = findRowCheckboxes();
                var start = checks.indexOf(lastChecked);
                var end = checks.indexOf(cb);
                if (start > -1 && end > -1){
                    if (start > end){ var tmp = start; start = end; end = tmp; }
                    // mirror the checked state of the clicked checkbox
                    var newState = !cb.checked;
                    for (var i = start; i <= end; i++){ if (!checks[i].disabled) checks[i].checked = newState; }
                    updateSelectedCount();
                    // ensure lastChecked follows the clicked checkbox
                    lastChecked = cb;
                    return;
                }
            }

            // Toggle single row checkbox as before
            cb.checked = !cb.checked;
            tr.classList.toggle('table-active', cb.checked);
            updateSelectedCount();
            lastChecked = cb;
        }, true);

        table.addEventListener('change', function(e){ if (e.target && e.target.matches && e.target.matches('input[type="checkbox"]')) updateSelectedCount(); }, true);

        toggleBtn.addEventListener('click', function(){
            selectionEnabled = !selectionEnabled;
            if (selectionEnabled){
                toggleBtn.textContent = 'Disable row selection';
                if (controlsEl) controlsEl.style.display = '';
                setCheckboxesDisabled(false);
                showSelectionColumn();
                try {
                    table.style.userSelect = 'none';
                    table.style.webkitUserSelect = 'none';
                    table.style.MozUserSelect = 'none';
                    table.classList && table.classList.add('selection-mode');
                } catch (e) {}
            } else {
                toggleBtn.textContent = 'Enable row selection';
                if (controlsEl) controlsEl.style.display = 'none';
                setCheckboxesDisabled(true);
                hideSelectionColumn();
                findRowCheckboxes().forEach(function(cb){ cb.checked = false; });
                updateSelectedCount();
                lastChecked = null;
                try {
                    table.style.userSelect = '';
                    table.style.webkitUserSelect = '';
                    table.style.MozUserSelect = '';
                    table.classList && table.classList.remove('selection-mode');
                } catch (e) {}
            }
        });

        if (selectAllBtn) selectAllBtn.addEventListener('click', function(){ findRowCheckboxes().forEach(function(cb){ if (!cb.disabled) cb.checked = true; }); updateSelectedCount(); lastChecked = null; });
        if (clearBtn) clearBtn.addEventListener('click', function(){ findRowCheckboxes().forEach(function(cb){ if (!cb.disabled) cb.checked = false; }); updateSelectedCount(); lastChecked = null; });

        updateSelectedCount();
    };
})();
</script>
"""

                # Replace the simple placeholder with the runtime token. Using
                # a plain triple-quoted string avoids f-string brace parsing errors
                # from the JavaScript code which contains many `{`/`}` characters.
                script = script.replace('__TOKEN__', token)

                return mark_safe(html + script)

class SingleImageColumn(tables.Column):
    def render(self, value):
        try:
            thumbnailer = get_thumbnailer(value)
            return format_html('<img src="/media/{}" />', thumbnailer["exam-list"])
        except InvalidImageFormatError:
            return format_html('<span title="{}">Invalid image url<span>', value)

class BlockImageColumn(tables.Column):
    def render(self, value):
        blocks = []
        for obj in value.all():
            blocks.append(obj.get_block())
        return mark_safe(
            """<span class='multi-image-block'>
            {}
            </span>""".format("".join(blocks))
        )

class SeriesImageColumn(tables.Column):
    def render(self, value):
        #obj = value.first()
        images = value.all()

        if not images:
            return mark_safe('<span>None</span><br/>')

        obj = findMiddle(images)

        if obj is None:
            return "None"

        image_object = obj.image
        try:
            thumbnailer = get_thumbnailer(image_object)
            return format_html(
                '<img src="/media/{}" /><br/>[{}]',
                thumbnailer["exam-list"],
                value.count(),
            )
        except InvalidImageFormatError:
            return format_html(
                '<span title="{}">Invalid image url<span><br/>[{}]',
                image_object,
                value.count(),
            )

class FirstImageColumn(tables.Column):
    def render(self, value):
        obj = value.all()

        if not obj:
            return mark_safe('<span>No image</span>')

        image_object = obj[0].image
        try:
            thumbnailer = get_thumbnailer(image_object)
            return format_html('<img src="/media/{}" />', thumbnailer["exam-list"])
        except InvalidImageFormatError:
            return format_html('<span title="{}">Invalid image url<span>', image_object)


class CidUserTable(SelectionTable):
    cid = tables.LinkColumn(
        "cid_scores", args=[A("cid"), A("passcode")], orderable=True
    )

    # physics_exams = tables.ManyToManyColumn(verbose_name="Physics Exams")
    # rapid_exams = tables.ManyToManyColumn(verbose_name="Rapid Exams")
    # sba_exams = tables.ManyToManyColumn(verbose_name="SBA Exams")
    # longs_exams = tables.ManyToManyColumn(verbose_name="Long Exams")
    # anatomy_exams = tables.ManyToManyColumn(verbose_name="Anatomy Exams")
    group = tables.Column(linkify=True)

    #selection = tables.CheckBoxColumn(accessor="pk", orderable=False)

    emails = tables.Column(empty_values=(), orderable=False)

    edit = tables.LinkColumn(
        "generic:update_cid", text="Edit", args=[A("pk")], orderable=True
    )

    class Meta(SelectionTable.Meta):
        model = CidUser
        template_name = "django_tables2/bootstrap4.html"
        fields = (
            # "cid",
            "passcode",
            "active",
            "group",
            "internal_candidate",
            # "email",
        )
        sequence = ("cid", "passcode", "active", "internal_candidate", "emails")

    def __init__(self, data=None, *args, **kwargs):
        super().__init__(
            data.prefetch_related("group"),
            *args,
            **kwargs,
        )

    def render_emails(self, value, record):
        print(self)
        if record.supervisor is not None:
            return format_html("""{}\n[{}]""", record.email, record.supervisor.email)
        else:
            return record.email


class CidUserExamTable(SelectionTable):
    cid = tables.LinkColumn(
        "cid_scores", args=[A("cid"), A("passcode")], orderable=True
    )

    physics_exams = tables.ManyToManyColumn(verbose_name="Physics Exams")
    rapid_exams = tables.ManyToManyColumn(verbose_name="Rapid Exams")
    sba_exams = tables.ManyToManyColumn(verbose_name="SBA Exams")
    longs_exams = tables.ManyToManyColumn(verbose_name="Long Exams")
    anatomy_exams = tables.ManyToManyColumn(verbose_name="Anatomy Exams")

    #selection = tables.CheckBoxColumn(accessor="pk", orderable=False)

    emails = tables.Column(empty_values=(), orderable=False)

    edit = tables.LinkColumn(
        "generic:update_cid", text="Edit", args=[A("pk")], orderable=True
    )

    class Meta(SelectionTable.Meta):
        model = CidUser
        template_name = "django_tables2/bootstrap4.html"
        fields = (
            # "cid",
            "passcode",
            "active",
            "group",
            "internal_candidate",
            # "email",
        )
        sequence = ("cid", "passcode", "active", "internal_candidate", "emails")

    def __init__(self, data=None, *args, **kwargs):
        super().__init__(
            data.prefetch_related(
                "physics_exams",
                "rapid_exams",
                "sba_exams",
                "anatomy_exams",
                "longs_exams",
                "group",
            ),
            *args,
            **kwargs,
        )

    def render_emails(self, value, record):
        if record.supervisor is not None:
            return format_html("""{}\n[{}]""", record.email, record.supervisor.email)
        else:
            return record.email


#    def render_cid(self, value, record):
#        return format_html("""<a href="#" onclick="return window.create_popup_window('/longs/series/{}', 'Series')" >Popup</a>""", record.pk)


class UserUserTable(SelectionTable):

    user = tables.Column(
        linkify={"viewname": "account_profile", "args": [A("username")]},
        orderable=True,
        verbose_name="User",
        empty_values=(),
    )

    supervisor = tables.Column(
        linkify={
            "viewname": "generic:supervisor_detail",
            "args": [A("userprofile__supervisor__pk")],
        },
        orderable=True,
        verbose_name="Supervisor",
        accessor=A("userprofile__supervisor"),
    )

    #selection = tables.CheckBoxColumn(accessor="pk", orderable=False)

    edit = tables.Column(
        empty_values=(),
        linkify={"viewname": "account_update", "args": [A("username")]},
        orderable=False,
        verbose_name="Edit",
    )
    admin_edit = tables.Column(
        empty_values=(),
        linkify={"viewname": "admin:auth_user_change", "args": [A("pk")]},
        orderable=False,
        verbose_name="Admin",
    )

    user_groups = tables.ManyToManyColumn(verbose_name="Groups", linkify_item=True)

    date_joined = tables.DateTimeColumn(format="Y/m/d")

    class Meta(SelectionTable.Meta):
        model = User
        template_name = "django_tables2/bootstrap4.html"
        fields = (
            # "cid",
            # "userprofile",
            "userprofile__grade",
            "date_joined",
            # "user_groups",
            # "userprofile.supervisor",
            # "email",
        )
        sequence = (
            "user",
            "userprofile__grade",
            "supervisor",
            "user_groups",
            "date_joined",
            "edit",
            "admin_edit",
        )

    def __init__(self, data=None, *args, **kwargs):
        super().__init__(
            data.prefetch_related(
                "userprofile",
                "user_groups",
                "userprofile__grade",
                "supervisor",
            ),
            *args,
            **kwargs,
        )

    # This should be avoidable?
    def render_edit(self, value, record):
        return "Edit"

    def render_admin_edit(self, value, record):
        return "Admin"

    def render_user(self, value, record):
        span_class = ""
        if record.userprofile.peninsula_trainee:
            span_class = "peninsula-trainee"
        return format_html(
            "<div style='flex-flow: row wrap; display: flex;'><div class='{}'></div><div>{} {}<br/>{}<br/>{}</div></div>",
            span_class,
            record.first_name,
            record.last_name,
            record.username,
            record.email,
        )

class ExaminationTable(SelectionTable):
    examination = tables.Column(
        linkify=("generic:examination_detail", {"pk": tables.A("pk")}),
        verbose_name="Condition",
    )

    #edit = tables.LinkColumn(
    #    "generic:examination_update", text="Edit", args=[A("pk")], orderable=False
    #)
    delete = tables.LinkColumn(
        "generic:examination_delete", text="Delete", args=[A("pk")], orderable=False
    )
    #selection = tables.CheckBoxColumn(accessor="pk", orderable=False)

    # synonym = tables.ManyToManyColumn(verbose_name="Synonyms")
    #synonym = tables.Column(empty_values=())
    #parent = tables.ManyToManyColumn(verbose_name="Parents")

    class Meta(SelectionTable.Meta):
        model = Examination
        template_name = "django_tables2/bootstrap4.html"
        #fields = ("primary", "subspecialty")
        sequence = ("examination",)

class SupervisorTable(SelectionTable):
    name = tables.Column(
        linkify=("generic:supervisor_detail", {"pk": tables.A("pk")}),
        verbose_name="Name",
    )

    # We don't use a link column as we want to add a redirect parameter
    edit = tables.Column(
        accessor="pk", orderable=False
    )

    class Meta(SelectionTable.Meta):
        model = Supervisor
        template_name = "django_tables2/bootstrap4.html"
        
        sequence = ("name", "email")
        exclude = ("id",)
        SelectionTable.Meta.attrs.update({"id": "supervisor-table"})

    def render_edit(self, value, record):
        url = f"{reverse("generic:supervisor_edit", args=[record.pk])}?redirect={self.request.path}"
        return format_html("<a href='{}'>Edit</a>", url)