# -*- coding: utf-8 -*-

# Copyright(C) 2010-2017 Théo Dorée
#
# This file is part of a woob module.
#
# This woob module is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This woob module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this woob module. If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

from woob.browser.pages import HTMLPage, LoggedPage
from woob.browser.filters.html import Attr
from woob.browser.filters.standard import CleanText, Env, Regexp, Format, Date
from woob.browser.elements import ListElement, ItemElement, method
from woob.capabilities import NotAvailable
from woob.capabilities.bill import Document
from woob.tools.date import parse_french_date


class DocumentsPage(LoggedPage, HTMLPage):
    @method
    class get_documents(ListElement):
        item_xpath = '//div[has-class("mawa-cards-item dashboard-item")]'

        class item(ItemElement):
            klass = Document

            obj_id = Format(
                '%s_%s',
                Env('subid'),
                Regexp(Attr('.', 'data-module-open-link--link'), r'#/details/(.*)'),
            )
            obj_format = 'pdf'
            # eg when formatted (not complete list):
            # - Situation de contrat suite à réajustement automatique Assurance Vie N° XXXXXXXXXX
            # - Lettre d'information client Assurance Vie N° XXXXXXXXXX
            # - Attestation de rachat partiel Assurance Vie N° XXXXXXXXXXXXXX
            obj_label = Format(
                '%s %s %s',
                CleanText('.//h3[@class="card-title"]'),
                CleanText('.//div[@class="sticker-content"]//strong'),
                CleanText('.//p[@class="contract-info"]'),
            )
            obj_date = Date(CleanText('.//p[@class="card-date"]'), parse_func=parse_french_date, default=NotAvailable)
            obj_type = 'document'
            obj_url = Attr('.', 'data-url')
            obj__download_id = Regexp(Attr('.', 'data-url'), r'.[dp]id_(.*?)\.', default=None)


class DownloadPage(LoggedPage, HTMLPage):
    def create_document(self):
        form = self.get_form(xpath='//form[has-class("form-download-pdf")]')
        form.submit()


class DocumentDetailsPage(LoggedPage, HTMLPage):
    def get_download_url(self):
        return Attr('//button', 'data-module-open-link--link')(self.doc)
