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

__author__ = 'stevenkaplan'
from sources.functions import *
from sefaria.model import *
from parsing_utilities.XML_to_JaggedArray import XML_to_JaggedArray
from sefaria.helper.schema import *
import bleach

SERVER = "http://proto.sefaria.org"

def create_schema():
    root = SchemaNode()
    root.add_primary_titles("Care of the Critically Ill",
                            u"תשובות רפואיות")

    arr = ['Preface / \xd7\x9e\xd7\x91\xd7\x95\xd7\x90',
           'A Biography of Greatness / \xd7\x91\xd7\x99\xd7\x95\xd7\x92\xd7\xa8\xd7\xa4\xd7\x99\xd7\x94 \xd7\xa9\xd7\x9c \xd7\x92\xd7\x93\xd7\x95\xd7\x9c\xd7\x94',
           "Responsa of Rav Moshe Feinstein / \xd7\xaa\xd7\xa9\xd7\x95\xd7\x91\xd7\x95\xd7\xaa \xd7\xa8' \xd7\x9e\xd7\xa9\xd7\x94 \xd7\xa4\xd7\x99\xd7\x99\xd7\xa0\xd7\xa9\xd7\x98\xd7\x99\xd7\x99\xd7\x9f",
           'Quality and Sanctity of Life in the Talmud and Midrash coauthored by Dr Fred Rosner / איכות חיים וקדושת החיים בתלמוד ובמדרש, נכתב יחד עם ד"ר פרד רוזנר']

    for count, line in enumerate(arr):
        en, he = line.split(" / ")
        he = he.decode('utf-8')
        en = make_title(en)
        if count < 2:
            node = JaggedArrayNode()
            node.add_structure(["Paragraph"])
        else:
            node = SchemaNode()
        node.add_primary_titles(en, he)
        root.append(node)

    children_end = ['Introduction / \xd7\x94\xd7\xa7\xd7\x93\xd7\x9e\xd7\x94',
                         'Treatment of the Dying in Judaism / \xd7\x98\xd7\x99\xd7\xa4\xd7\x95\xd7\x9c \xd7\x91\xd7\xa0\xd7\x95\xd7\x98\xd7\x94 \xd7\x9c\xd7\x9e\xd7\x95\xd7\xaa \xd7\x91\xd7\x99\xd7\x94\xd7\x93\xd7\x95\xd7\xaa',
                         'Recent Rabbinic Rulings / \xd7\xa4\xd7\xa1\xd7\x99\xd7\xa7\xd7\x95\xd7\xaa \xd7\x94\xd7\x9c\xd7\x9b\xd7\x94 \xd7\x97\xd7\x93\xd7\xa9\xd7\x95\xd7\xaa',
                         'Quality of Life in Classic Jewish Sources / \xd7\x90\xd7\x99\xd7\x9b\xd7\x95\xd7\xaa \xd7\x97\xd7\x99\xd7\x99\xd7\x9d \xd7\x91\xd7\x9e\xd7\xa7\xd7\x95\xd7\xa8\xd7\x95\xd7\xaa \xd7\x94\xd7\xa7\xd7\x9c\xd7\x90\xd7\xa1\xd7\x99\xd7\x99\xd7\x9d \xd7\xa9\xd7\x9c \xd7\x94\xd7\x99\xd7\x94\xd7\x93\xd7\x95\xd7\xaa', 'Physical Pain / \xd7\x9b\xd7\x90\xd7\x91 \xd7\xa4\xd7\x99\xd7\x96\xd7\x99', 'Mental Anguish / \xd7\x99\xd7\xa1\xd7\x95\xd7\xa8\xd7\x99 \xd7\xa0\xd7\xa4\xd7\xa9',
                         'Summary / \xd7\xa1\xd7\x99\xd7\x9b\xd7\x95\xd7\x9d']

    for child in children_end:
        en, he = child.split(" / ")
        he = he.decode('utf-8')
        en = make_title(en)
        node = JaggedArrayNode()
        node.add_primary_titles(en, he)
        node.add_structure(["Paragraph"])
        root.children[3].append(node)


    children_responsa = ['Introduction / \xd7\x94\xd7\xa7\xd7\x93\xd7\x9e\xd7\x94',
 'Preface to Iggeros Moshe, Volume I / \xd7\x9e\xd7\x91\xd7\x95\xd7\x90 \xd7\x9c\xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x97\xd7\x9c\xd7\xa7 \xd7\x90 (\xd7\xaa\xd7\xa9\xd7\x99\xe2\x80\x9d\xd7\x98)',
 "Iggeros Moshe, Yoreh De'ah III 132 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x99\xd7\x95\xd7\xa8\xd7\x94 \xd7\x93\xd7\xa2\xd7\x94,\xd7\x92, \xd7\xa7\xd7\x9c\xd7\x91",
 'A New Teshuvah / \xd7\xaa\xd7\xa9\xd7\x95\xd7\x91\xd7\x94 \xd7\x97\xd7\x93\xd7\xa9\xd7\x94',
 'Iggeros Moshe, Choshen Mishpat II 72 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x97\xd7\x95\xd7\xa9\xd7\x9f \xd7\x9e\xd7\xa9\xd7\xa4\xd7\x98, \xd7\x91, \xd7\xa2\xd7\x91',
 'Iggeros Moshe, Choshen Mishpat II 73 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x97\xd7\x95\xd7\xa9\xd7\x9f \xd7\x9e\xd7\xa9\xd7\xa4\xd7\x98, \xd7\x91, \xd7\xa2\xd7\x92',
 'Iggeros Moshe, Choshen Mishpat II 74 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x97\xd7\x95\xd7\xa9\xd7\x9f \xd7\x9e\xd7\xa9\xd7\xa4\xd7\x98, \xd7\x91, \xd7\xa2\xd7\x93',
 'Iggeros Moshe, Choshen Mishpat II 75 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x97\xd7\x95\xd7\xa9\xd7\x9f \xd7\x9e\xd7\xa9\xd7\xa4\xd7\x98, \xd7\x91, \xd7\xa2\xd7\x94',
 'The Anatomy of a Teshuvah / \xd7\x90\xd7\xa0\xd7\x98\xd7\x95\xd7\x9e\xd7\x99\xd7\x94 \xd7\xa9\xd7\x9c \xd7\xaa\xd7\xa9\xd7\x95\xd7\x91\xd7\x94',
 'Talmudic Sources / \xd7\x9e\xd7\xa7\xd7\x95\xd7\xa8\xd7\x95\xd7\xaa \xd7\xaa\xd7\x9c\xd7\x9e\xd7\x95\xd7\x93\xd7\x99\xd7\x99\xd7\x9d',
 'Analysis of The Talmudic Sources / \xd7\xa0\xd7\x99\xd7\xaa\xd7\x95\xd7\x97 \xd7\x94\xd7\x9e\xd7\xa7\xd7\x95\xd7\xa8\xd7\x95\xd7\xaa \xd7\x94\xd7\xaa\xd7\x9c\xd7\x9e\xd7\x95\xd7\x93\xd7\x99\xd7\x99\xd7\x9d ',
 'Later Sources / \xd7\x9e\xd7\xa7\xd7\x95\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\x90\xd7\x95\xd7\x97\xd7\xa8\xd7\x99\xd7\x9d',
 'Quality and Sanctity of Life, A Torah View / \xd7\x90\xd7\x99\xd7\x9b\xd7\x95\xd7\xaa \xd7\x97\xd7\x99\xd7\x99\xd7\x9d \xd7\x95\xd7\xa7\xd7\x93\xd7\x95\xd7\xa9\xd7\xaa \xd7\x94\xd7\x97\xd7\x99\xd7\x99\xd7\x9d  \xd7\x9e\xd7\x91\xd7\x98 \xd7\xa9\xd7\x9c \xd7\xaa\xd7\x95\xd7\xa8\xd7\x94',
 'Additional Note / \xd7\x94\xd7\xa2\xd7\xa8\xd7\x94 \xd7\x9e\xd7\xa9\xd7\x9c\xd7\x99\xd7\x9e\xd7\x94',
 'Clarification of the Halakhic Rulings of Rav Moshe Feinstein and of Rav Shlomo Zalman Auerbach in Regard to Brain Death / \xd7\x94\xd7\x91\xd7\x94\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9c\xd7\x92\xd7\x91\xd7\x99 \xd7\xa4\xd7\xa1\xd7\x99\xd7\xa7\xd7\xaa \xd7\x94\xd7\x94\xd7\x9c\xd7\x9b\xd7\x94 \xd7\xa9\xd7\x9c \xd7\xa8\' \xd7\x9e\xd7\xa9\xd7\x94 \xd7\xa4\xd7\x99\xd7\x99\xd7\xa0\xd7\xa9\xd7\x98\xd7\x99\xd7\x99\xd7\x9f \xd7\x95\xd7\xa8\' \xd7\xa9\xd7\x9c\xd7\x9e\xd7\x94 \xd7\x96\xd7\x9c\xd7\x9e\xd7\x9f \xd7\x90\xd7\x95\xd7\x99\xd7\xa8\xd7\x91\xd7\x9a \xd7\x96\xd7\xa6"\xd7\x9c \xd7\x91\xd7\xa2\xd7\xa0\xd7\x99\xd7\x99\xd7\x9f \xd7\x9e\xd7\x95\xd7\x95\xd7\xaa \xd7\x9e\xd7\x95\xd7\x97\xd7\x99',
 "Iggeros Moshe, Yoreh De'ah I 145 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x99\xd7\x95\xd7\xa8\xd7\x94 \xd7\x93\xd7\xa2\xd7\x94, \xd7\x90, \xd7\xa7\xd7\x9e\xd7\x94",
 'Introductory Note / \xd7\x94\xd7\xa2\xd7\xa8\xd7\x94 \xd7\x9e\xd7\xa7\xd7\x93\xd7\x99\xd7\x9e\xd7\x94',
 'Iggeros Moshe, Orach Chaim IV 79 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x90\xd7\x95\xd7\xa8\xd7\x97 \xd7\x97\xd7\x99\xd7\x99\xd7\x9d, \xd7\x93, \xd7\xa2\xd7\x98',
 'Iggeros Moshe, Orach Chaim IV 81 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x90\xd7\x95\xd7\xa8\xd7\x97 \xd7\x97\xd7\x99\xd7\x99\xd7\x9d, \xd7\x93, \xd7\xa4\xd7\x90',
 'Iggeros Moshe, Orach Chaim IV 80 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x90\xd7\x95\xd7\xa8\xd7\x97 \xd7\x97\xd7\x99\xd7\x99\xd7\x9d, \xd7\x93, \xd7\xa4',
 "Iggeros Moshe, Yoreh De'ah II 174 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x99\xd7\x95\xd7\xa8\xd7\x94 \xd7\x93\xd7\xa2\xd7\x94, \xd7\x91, \xd7\xa7\xd7\xa2\xd7\x93",
 "Iggeros Moshe, Yoreh De'ah II 166 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x99\xd7\x95\xd7\xa8\xd7\x94 \xd7\x93\xd7\xa2\xd7\x94, \xd7\x91, \xd7\xa7\xd7\xa1\xd7\x95",
 "Iggeros Moshe, Yoreh De'ah II 164 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x99\xd7\x95\xd7\xa8\xd7\x94 \xd7\x93\xd7\xa2\xd7\x94, \xd7\x91, \xd7\xa7\xd7\xa1\xd7\x93",
 "Iggeros Moshe, Yoreh De'ah II 162 / \xd7\x90\xd7\x92\xd7\xa8\xd7\x95\xd7\xaa \xd7\x9e\xd7\xa9\xd7\x94 \xd7\x99\xd7\x95\xd7\xa8\xd7\x94 \xd7\x93\xd7\xa2\xd7\x94, \xd7\x91, \xd7\xa7\xd7\xa1\xd7\x91",
 'Unpublished Responsum "So One May Live" / \xd7\xaa\xd7\xa9\xd7\x95\xd7\x91\xd7\x94 \xd7\xa9\xd7\x9c\xd7\x90 \xd7\xa4\xd7\x95\xd7\xa8\xd7\xa1\xd7\x9e\xd7\x94 "\xd7\x9b\xd7\x93\xd7\x99 \xd7\xa9\xd7\x99\xd7\x97\xd7\x99\xd7\x94 \xd7\x94\xd7\x90\xd7\x97\xd7\x93"']

    for child in children_responsa:
        en, he = child.split(" / ")
        he = he.decode('utf-8')
        node = JaggedArrayNode()
        en = make_title(en)
        node.add_primary_titles(en, he)
        node.add_structure(["Paragraph"])
        root.children[2].append(node)

    root.validate()
    index = {
        "schema": root.serialize(),
        "title": "Care of the Critically Ill",
        "categories": ["Modern Works"],
    }
    post_index(index, server=SERVER)




def reorder_modify(text):
    return bleach.clean(text, strip=True)

if __name__ == "__main__":
    post_term({
        "name": "R' Moshe Feinstein",
        "scheme": "toc_categories",
        "titles":
        [
            {
            "text": "R' Moshe Feinstein",
            "lang": "en",
            "primary": True
            },
            {
             "text": u"ר׳ משה פיינשטיין‎‎",
            "lang": "he",
            "primary": True
            }
        ]
    }, server=SERVER)

    #create_schema()
    post_info = {}
    post_info["language"] = "en"
    post_info["server"] = SERVER
    allowed_tags = ["book", "intro", "part", "chapter", "p", "ftnote", "title", "ol", "footnotes", "appendix"]
    allowed_attributes = ["id"]
    p = re.compile("\d+a?\.")

    post_info["versionTitle"] = "Responsa of Rav Moshe Feinstein, trans. by Moshe David Tendler; Vol. 1: Care of the Critically Ill, Hoboken, NJ: Ktav, 1996."
    post_info["versionSource"] = "http://www.ktav.com/index.php/responsa-of-rav-moshe-feinstein.html"
    title = "Care of the Critically Ill"
    file_name = "responsa.xml"

    parser = XML_to_JaggedArray(title, file_name, allowed_tags, allowed_attributes, post_info, change_name=True, image_dir="./images",
                                titled=True)
    parser.set_funcs(reorder_test=lambda x: x.tag == "title" and x.text.find("<bold>") == 0, reorder_modify=reorder_modify)
    parser.run()

