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

from __future__ import unicode_literals

# #############################################################################
# ################################   Notes   ##################################
# #############################################################################
"""
1. run the script
2. acces to the codes by listofcodes: for the 'x'th code:
    listofcodes[x][0] or listofcodes[x][2].name: code's title
    listofcodes[x][1]: ref legifrance
    listofcodes[x][2]: the code

*listofcodes[x][2].contents[] will return the main titles of the codes
*listofcodes[x][2].contents[0].contents[] will return the rank 2 titles or
the Articles contained in the first main title of the code
*etc.
"""
# #############################################################################

try:
    from dl_law_codes.classe_pdf import Code_de_lois, Article, Titre, Table
    from dl_law_codes.functions import remove_piece_of_text, get_something
    from models import LawCode, LawArticle, CodeBlock
    from stock_update import SU_Article
except:
    from CreateYourLaws.dl_law_codes.classe_pdf import Code_de_lois, Article
    from CreateYourLaws.dl_law_codes.classe_pdf import Titre, Table
    from CreateYourLaws.dl_law_codes.functions import get_something
    from CreateYourLaws.dl_law_codes.fun
ctions import remove_piece_of_text
    from CreateYourLaws.models import LawCode, LawArticle
    from CreateYourLaws.models import CodeBlock, CYL_user
    from CreateYourLaws.dl_law_codes.stock_update import SU_Article

import ssl
import urllib.error
import urllib.request
import urllib.parse


def get_code_data(code_LEGITEXT, url_fail, Gov):  # 2
    """ Recover the data (titles and articles) from a specified code
    (param code LEGIT
EXT corresponding on Legifrance) """
    url = "https://www.legifrance.gouv.fr/affichCode.do?cidTexte="\
        + "LEGITEXT" + code_LEGITEXT
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    try:
        a = urllib.request.urlopen(url, context=ctx)
        code_source = a.read().decode("utf-8")
    except:
        url_fail.append(('code', code_LEGITEXT))
        return url_fail
    code = Code_de_lois()
    pos_counter = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    buf_15 = ""
    list_title_open = []
    for i in range(len(code_source)):
        buf_15 += code_source[i]
        if len(buf_15) == 15:
            # --------- Code title recovering ---------------
            if buf_15 == 'd="titreTexte">':
                code.name, i = get_title_code(code_source, i)
                code.name = code.name.replace("\r", "")
                code.name = code.name.replace("\t", "")
                try:
                    Law_Code, created = LawCode.objects.get_or_create(
                        title=code.name)
                    Law_Code.updated = True
                    Law_Code.save()
                except:
                    print("Impossible to get/create the law code: ", code.name)
            # ------- paragraph title recovering ------------
            if buf_15 == '<span class="TM':
                title, i = get_title(code_source, i)
                if title.rank == 1:
                    code.contents.append(title)
                    pos_counter[2] = 0
                    list_title_open.clear()
                else:
                    for x in range(len(list_title_open) - 1, 0, -1):
                        if list_title_open[x].rank >= title.rank:
                            if list_title_open[x].rank > title.rank:
                                pos_counter[list_title_open[x].rank] = 0
                            del list_title_open[x]
                    list_title_open[-1].contents.append(title)
                pos_counter[title.rank] += 1
                try:
                    new_title, created = CodeBlock.objects.get_or_create(
                        title=title.name,
                        position=pos_counter[title.rank],
                        rank=title.rank,
                        law_code=Law_Code)
                    title.idsql = new_title.id
                    if (title.rank > 1 and
                            list_title_open[-1].idsql < title.idsql):
                        new_title.block = CodeBlock.objects.get(
                            title=list_title_open[-1].name,
                            rank=list_title_open[-1].rank,
                            id=list_title_open[-1].idsql,
                            law_code=Law_Code)
                    new_title.updated = True
                    new_title.save()
                except:
                    print("Impossible to get/create the block code",
                          " title: ", title.name,
                          "(rank: ", title.rank, ")",
                          "from: ", code.name)
                list_title_open.append(title)
            # -- Articles contained in the last paragraph Title recovering --
            if buf_15 == '="codeLienArt">':
                link, i = get_link_articles(code_source, i)
                url_fail, several_articles = get_article(link,
                                                         code.name,
                                                         new_title,
                                                         url_fail)
                for x in range(len(several_articles)):
                    try:
                        SU_Article(several_articles[x],
                                   link,
                                   new_title,
                                   Law_Code,
                                   Gov)
                    except Exception:
                        try:
                            art, created = LawArticle.objects.get_or_create(
                                autor=Gov,
                                title=several_articles[x].name,
                                law_code=Law_Code,
                                block=new_title)
                            art.updated = False
                            art.url = link
                            art.save()
                        except Exception:
                            print("Unable to create or get the instance of ",
                                  "LawArticle from",
                                  several_articles[x].name,
                                  "from law code: ",
                                  Law_Code.title)
                # Ligne suivante à surveiller si pb stockage dans les blocks
                list_title_open[-1].contents = several_articles
            buf_15 = buf_15[1:15]
    return url_fail


def get_article(link, code, block, url_fail):
    """ detect, select and copy all articles from a Legifrance page """
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    contents = []
    try:
        b = urllib.request.urlopen(link, context=ctx)
        cs_article = b.read().decode("utf-8")
    except:
        url_fail.append(("article", link, code, block))
        return url_fail, contents
    new_article = Article()
    buf = ""
    for j in range(len(cs_article)):
        buf += cs_article[j]
        if len(buf) >= 21:
            if buf == '<div class="article">':
                new_article.name, j = get_something(cs_article,
                                                    '<div class="titreArt">',
                                                    '<a',
                                                    j)
                try:
                    new_article.html, j = get_something(cs_article,
                                                        'class="corpsArt">',
                                                        '</div>',
                                                        j)
                    new_article.contents = recover_article(new_article.html)
                    contents.append(new_article)
           