import tkinter as tk
import csv
import os.path

def Create_file():
  my_file = e1.get()
  if os.path.isfile(my_file):
    f = open("AlbumEasy.txt", "w")
  #Country = "Iceland" 
    Country = e2.get()
    my_file = e1.get()
  
    OLine = "ALBUM_PAGES_SIZE (210.0 297.0)    #Set the album page size to A4\n"
    f.write(OLine)
    OLine = "ALBUM_PAGES_MARGINS  (22.0 12.0 10.0 10.0)\n"
    f.write(OLine)
    OLine = "ALBUM_PAGES_BORDER   (0.1 0.5 0.1 1.0)\n"
    f.write(OLine)
    OLine = "ALBUM_PAGES_SPACING  (1.0 4.0)\n"
    f.write(OLine)
    OLineT = "ALBUM_PAGES_TITLE (TB 20 \"{}\" )\n"
    f.write(OLineT.format(Country))
    OLine = "ALBUM_STAMP_IMG_SETTING (16.0 16.0 true)\n"
    f.write(OLine)
    OLine = "ROW_ALIGN_MIDDLE\n"
    f.write(OLine)
    OLine = "\n"
    f.write(OLine)
    OLine = "#PAGE_START_VAR (1.0 3.5)\n"
    f.write(OLine)
    OLine = "#STAMP_ADD (0 0 \" \" \" \" \" \" \" \")\n"
    f.write(OLine)

    PC = 1
    OLineP = "\nPAGE_START #{}\n"
    f.write(OLineP.format(PC))
    f.close()
    OLineS = "STAMP_ADD_IMG ({} {} \"images/{}\"  \"   \"   \"\\n{} {}\\n{}\" \" \")\n"
    OLineC = "PAGE_TEXT_CENTRE (HB 12 \"{}\\n\");\n"
    OLineH = "STAMP_HEADING (HB 12 \"{}\\n\")\n"
    OLineL = "ROW_START_FS (CB 6 0.1 6.0)\n"
    OLineB = "STAMP_ADD (0 0 \" \" \" \" \" \" \" \")\n"

    Rcount = 0
    Hcount = 0
    rslinec = 0
    HC = 0
    WC = 0
    PS = " "

    output = open("AlbumEasy.txt", "a", encoding='utf-8')
#    with open('Stamps.csv', 'r') as csvfile:
    with open(my_file, 'r') as csvfile:

      csvreader = csv.reader(csvfile, delimiter=',' )

      for line in csvreader:
          Series, Desc, Color, Face, Wide, High, Image, Ltype = line
#        print(Series, Desc, Color, Face, Wide, High, Image, Ltype)
          WC = 8 + int(Wide)
          HC = 8 + int(High)
          Rcount = Rcount + WC + 6
          CS = (Series)
          if PS != CS:
            Hcount = Hcount + HC     
#          Rcount = 0

            if (Ltype) != "H":
              output.write(OLineC.format(Series.rstrip().title()))   # Page_Text_Centre

            output.write(OLineL)                # Row_Start
            PS = Series
        
          output.write(OLineS.format(WC, HC, Image, Face, Color.title(), Desc.rstrip().title()))   # Stamp_Add
          if (Ltype) != "C":
            output.write(OLineH.format(Series.rstrip().title()))  # Stamp_Heading
            output.write(OLineB)                 # Blank Stamp

          if Rcount > 155: 
             Hcount = Hcount + HC
             output.write(OLineL)               # Row_Start
             Rcount = 0

          if Hcount > 150:
            PC = PC + 1
            output.write(OLineP.format(PC))     # Page_Start
            output.write(OLineL)                # Row_Start
            Hcount = 0
            if (Ltype) != "H":
              output.write(OLineC.format(Series.rstrip().title()))   # Page_Text_Centre
              output.write("ROW_START_FS (CB 6 0.1 6.0)\n")

#    e1.delete(0, tk.END)
#    e2.delete(0, tk.END)

master = tk.Tk()
master.title(" CSV file to AlbumEasy File ")
master.geometry("370x340")
tk.Label(master, text="CSV File Name").grid(row=0)
tk.Label(master, text="Country").grid(row=1)
image = tk.PhotoImage(file="Cover.gif")
tk.Label(master, image=image).grid(row=5)

e1 = tk.Entry(master)
e2 = tk.Entry(master)
e1.insert(10, "Stamps.csv")
e2.insert(10, "Iceland")

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

tk.Button(master, 
          text='Quit', command=master.quit).grid(row=3, column=1, sticky=tk.W, pady=2)
tk.Button(master, text='Create', command=Create_file).grid(row=3, sticky=tk.W, pady=2)

master.mainloop()