# -*- coding: utf-8 -*-
import sys
f = sys.argv[1]
s = open(f, encoding="utf-8").read()
T = "\t"

def rep(old, new, s):
    if s.count(old) != 1:
        print("ANCHOR PROBLEM:", repr(old[:55]), "count=", s.count(old)); sys.exit(1)
    return s.replace(old, new, 1)

if "fermiDrawAffaireBanner" in s:
    print("banner already present, skipping"); sys.exit(0)

method = (
    T + "/**\n"
    + T + " * FERMI : bandeau charcoal Affaire/Marche au-dessus du tableau\n"
    + T + " * @param TCPDF $pdf\n"
    + T + " * @param Facture $object\n"
    + T + " * @param Translate $outputlangs\n"
    + T + " * @return void\n"
    + T + " */\n"
    + T + "protected function fermiDrawAffaireBanner(&$pdf, $object, $outputlangs)\n"
    + T + "{\n"
    + T*2 + "$x = $this->marge_gauche;\n"
    + T*2 + "$w = $this->page_largeur - $this->marge_gauche - $this->marge_droite;\n"
    + T*2 + "$ymark = $this->tab_top_newpage - 14;\n"
    + T*2 + "$affaire = '';\n"
    + T*2 + "if (!empty($object->fk_project)) {\n"
    + T*3 + "$object->fetchProject();\n"
    + T*3 + "if (!empty($object->project)) {\n"
    + T*4 + "$affaire = $object->project->ref;\n"
    + T*4 + "if (!empty($object->project->title)) { $affaire .= ' : '.$object->project->title; }\n"
    + T*3 + "}\n"
    + T*2 + "}\n"
    + T*2 + "$pdf->RoundedRect($x, $ymark, $w, 5, $this->corner_radius, '1111', 'F', array(), array(65, 65, 65));\n"
    + T*2 + "$pdf->SetTextColor(255, 255, 255);\n"
    + T*2 + "$pdf->SetFont('', 'B', 9);\n"
    + T*2 + "$pdf->SetXY($x + 1.5, $ymark + 0.5);\n"
    + T*2 + "$pdf->MultiCell($w - 3, 4, $outputlangs->convToOutputCharset('Affaire n° '.$affaire), 0, 'L', false);\n"
    + T*2 + "$marche = (string) $object->ref_client;\n"
    + T*2 + "$datestart = '';\n"
    + T*2 + "if (!empty($object->project) && !empty($object->project->date_start)) {\n"
    + T*3 + "$datestart = ' du '.dol_print_date($object->project->date_start, 'day', false, $outputlangs);\n"
    + T*2 + "}\n"
    + T*2 + "$pdf->SetTextColor(80, 80, 80);\n"
    + T*2 + "$pdf->SetFont('', '', 8);\n"
    + T*2 + "$pdf->SetXY($x + 1.5, $ymark + 5.2);\n"
    + T*2 + "$pdf->MultiCell($w - 3, 4, $outputlangs->convToOutputCharset('Marché n° '.$marche.$datestart), 0, 'L', false);\n"
    + T*2 + "$pdf->SetTextColor(0, 0, 0);\n"
    + T + "}\n\n"
)
anchor_m = T + "public function getInfosLineLastSituation(&$object, &$current_line)"
s = rep(anchor_m, method + anchor_m, s)

anchor_tt = "$this->tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);"
s = rep(anchor_tt, anchor_tt + "\n" + T*4 + "$this->tab_top_newpage += 16; // FERMI : place bandeau Affaire/Marche", s)

anchor_b = (
    T*4 + "$pdf->setPage(2);\n"
    + T*4 + "$pagenb++;\n"
    + T*4 + "$this->_pagehead($pdf, $object, 0, $outputlangs, $outputlangsbis);"
)
s = rep(anchor_b, anchor_b + "\n" + T*4 + "$this->fermiDrawAffaireBanner($pdf, $object, $outputlangs);", s)

open(f, "w", encoding="utf-8").write(s)
print("banner added to", f.split('/')[-1])
