;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; 
;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Procedure SaveMovieData()
  
  ;------------------------------------------------------------------------------------------------
  ; Get and overwrite the Movie structure from the form
  ;------------------------------------------------------------------------------------------------
  
  Movie\Title.s       = GetGadgetText(#Gadget_Dataform_Title)
  
  Movie\Year.s        = GetGadgetText(#Gadget_Dataform_Year)
  
  Movie\Rated.s       = GetGadgetText(#Gadget_Dataform_Rated)
  
  Movie\Released.s    = GetGadgetText(#Gadget_Dataform_Released)
  
  Movie\Runtime.s     = GetGadgetText(#Gadget_Dataform_RunTime)
  
  Movie\Genre.s       = GetGadgetText(#Gadget_Dataform_Genre)
  
  Movie\Director.s    = GetGadgetText(#Gadget_Dataform_Director)
  
  Movie\Writer.s      = GetGadgetText(#Gadget_Dataform_Writer)
  
  Movie\Actors.s      = GetGadgetText(#Gadget_Dataform_Actors)
  
  Movie\Plot.s        = GetGadgetText(#Gadget_Dataform_Plot)
  
  Movie\Language.s    = GetGadgetText(#Gadget_Dataform_Language)
  
  Movie\Country.s     = GetGadgetText(#Gadget_Dataform_Country)
  
  Movie\Awards.s      = GetGadgetText(#Gadget_Dataform_Awards)
  
  Movie\Poster.s      = GetGadgetText(#Gadget_Dataform_Poster)
  
  For GatherRatings.i = 0 To CountGadgetItems(#Gadget_Dataform_Ratings) - 1
    
    CurrentSource.s = GetGadgetItemText(#Gadget_Dataform_Ratings, GatherRatings.i, #RatingListRatingColumn)  
    
    CurrentValue.s  = GetGadgetItemText(#Gadget_Dataform_Ratings, GatherRatings.i, #RatingListValueColumn)  
    
    RatingsString.s + CurrentSource.s  + "/" +  CurrentValue.s  + "|"
    
  Next  GatherRatings.i
  
  RatingsString.s = Trim(RatingsString.s, ",")
  
  RatingsString.s = Trim(RatingsString.s, "|")
  
  Movie\Metascore.s   = GetGadgetText(#Gadget_Dataform_Metascore)
  
  Movie\ImdbRating.s  = GetGadgetText(#Gadget_Dataform_ImdbRating)
  
  Movie\ImdbVotes.s   = GetGadgetText(#Gadget_Dataform_ImdbVotes)
  
  Movie\ImdbID.s      = GetGadgetText(#Gadget_Dataform_ImdbID)
  
  Movie\Type.s        = GetGadgetText(#Gadget_Dataform_ImdbObject)
  
  Movie\DVD.s         = GetGadgetText(#Gadget_Dataform_DVD)
  
  Movie\Boxoffice.s   = GetGadgetText(#Gadget_Dataform_Boxoffice)
  
  Movie\Production.s  = GetGadgetText(#Gadget_Dataform_Production)
  
  Movie\Website.s     = GetGadgetText(#Gadget_Dataform_Website)
  
  ;Movie\Recordid.s    = GetGadgetText(#Gadget_Dataform_Recordid)
  
  ;------------------------------------------------------------------------------------------------
  ; Set the database strings to be used for Add or Edit modes.
  ;------------------------------------------------------------------------------------------------
  
  SetDatabaseString(Program\DatabaseHandle,  0, Movie\Title.s)
  
  SetDatabaseString(Program\DatabaseHandle,  1, Movie\Year.s)
  
  SetDatabaseString(Program\DatabaseHandle,  2, Movie\Rated.s)
  
  SetDatabaseString(Program\DatabaseHandle,  3, Movie\Released.s)
  
  SetDatabaseString(Program\DatabaseHandle,  4, Movie\Runtime.s)
  
  SetDatabaseString(Program\DatabaseHandle,  5, Movie\Genre.s)
  
  SetDatabaseString(Program\DatabaseHandle,  6, Movie\Director.s)
  
  SetDatabaseString(Program\DatabaseHandle,  7, Movie\Writer.s)
  
  SetDatabaseString(Program\DatabaseHandle,  8, Movie\Actors.s)
  
  SetDatabaseString(Program\DatabaseHandle,  9, Movie\Plot.s)
  
  SetDatabaseString(Program\DatabaseHandle, 10, Movie\Language.s)
  
  SetDatabaseString(Program\DatabaseHandle, 11, Movie\Country.s)
  
  SetDatabaseString(Program\DatabaseHandle, 12, Movie\Awards.s)
  
  SetDatabaseString(Program\DatabaseHandle, 13, Movie\Poster.s)
  
 ;SetDatabaseString(Program\DatabaseHandle, 14, Movie\Ratings.s)
  
  SetDatabaseString(Program\DatabaseHandle, 14, RatingsString.s)
  
  SetDatabaseString(Program\DatabaseHandle, 15, Movie\Metascore.s)
  
  SetDatabaseString(Program\DatabaseHandle, 16, Movie\ImdbRating.s)
  
  SetDatabaseString(Program\DatabaseHandle, 17, Movie\ImdbVotes.s)
  
  SetDatabaseString(Program\DatabaseHandle, 18, Movie\ImdbID.s)
  
  SetDatabaseString(Program\DatabaseHandle, 19, Movie\Type.s)
  
  SetDatabaseString(Program\DatabaseHandle, 20, Movie\DVD.s)
  
  SetDatabaseString(Program\DatabaseHandle, 21, Movie\BoxOffice.s)
  
  SetDatabaseString(Program\DatabaseHandle, 22, Movie\Production.s)
  
  SetDatabaseString(Program\DatabaseHandle, 23, Movie\Website.s)
  
 ;SetDatabaseString(Program\DatabaseHandle, 24, Movie\Recordid.s)
  
  ;------------------------------------------------------------------------------------------------
  ; 
  ;------------------------------------------------------------------------------------------------
  
  Select Program\LastWindow
      
    ;----------------------------------------------------------------------------------------------
    ; 
    ;----------------------------------------------------------------------------------------------
      
    Case  "Add New Movie"
      
      ;--------------------------------------------------------------------------------------------
      ; 
      ;--------------------------------------------------------------------------------------------
      
      If Movie\Title.s <> #Empty$ And Movie\ImdbID.s <> #Empty$  And Movie\Title.s  <> ">> Error : Movie not found! <<"   And Left(Movie\ImdbID.s, 2) = "tt"
        
        ;------------------------------------------------------------------------------------------
        ; 
        ;------------------------------------------------------------------------------------------
        
        InsertString.s            + 

        "INSERT INTO db_movies("  + 

        "title, "                 + 

        "year, "                  + 

        "rated, "                 + 

        "released, "              + 

        "runtime, "               + 

        "genre, "                 + 

        "director, "              + 

        "writer, "                + 

        "actors, "                + 

        "plot, "                  + 

        "language, "              + 

        "country, "               + 

        "awards, "                + 

        "poster, "                + 

        "ratings, "               + 

        "metascore, "             + 

        "imdbrating, "            + 

        "imdbvotes, "             + 

        "imdbid, "                + 

        "type, "                  + 

        "dvd, "                   + 

        "boxoffice, "             + 

        "production, "            + 

        "website) "               + 

        "VALUES("                 + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?, "                     + 

        "?)" 
        
        If DatabaseUpdate(Program\DatabaseHandle, InsertString.s) <> #DatabaseUpdateFail
          
          CurrentRecordid.s = DatabaseLastInsertRowId()
          
          If CurrentRecordid.s <> #Empty$
            
            Movie\Recordid.s = CurrentRecordid.s
            
            DisplayTitleString(#False, "AddMode")
          
            If Program\PictureDir + Program\PictureFilename  <> #Empty$
              
              PosterFileHandle.i = ReadFile(#PB_Any, Program\PictureDir + Program\PictureFilename)
              
              If PosterFileHandle.i
                
                PosterFileInSize.i = Lof(PosterFileHandle.i)
                
                *PosterBlobBuffer = AllocateMemory(PosterFileInSize.i)
                
                If *PosterBlobBuffer
                  
                  If ReadData(PosterFileHandle.i, *PosterBlobBuffer, PosterFileInSize.i) = PosterFileInSize.i
                    
                    SetDatabaseString(Program\DatabaseHandle, 0, CurrentRecordid.s)
                    
                    SetDatabaseString(Program\DatabaseHandle, 1, Program\PictureFilename)
                    
                    SetDatabaseBlob(Program\DatabaseHandle,   2, *PosterBlobBuffer, PosterFileInSize.i)
                    
                    DatabaseUpdate.s = "INSERT INTO db_moviesblobs("  + 

                    "moviesid, "                                      + 

                    "filename, "                                      + 

                    "filedata) "                                      + 

                    "VALUES (?, ?, ?)"
                    
                    If DatabaseUpdate(Program\DatabaseHandle, DatabaseUpdate.s) <> #DatabaseUpdateFail
                      
                      NewRecord.s = DatabaseLastInsertRowId()
                      
                      If NewRecord.s <> #Empty$
                        
                        SetInfoBarArea("Headings", "Error", "Got a new record number for picture data", #PB_Compiler_Procedure)
                        
                      Else
                        
                        SetInfoBarArea("Headings", "Error", "Could not get a new record number for picture data", #PB_Compiler_Procedure)
                        
                      EndIf
                      
                    Else
                      
                      SetInfoBarArea("Headings", "Error", "Could not save the picture data into the database", #PB_Compiler_Procedure)
                      
                    EndIf
                    
                  Else
                    
                    SetInfoBarArea("Headings", "Error", "Could not read the picture data into the buffer", #PB_Compiler_Procedure)
                    
                  EndIf
                  
                  FreeMemory(*PosterBlobBuffer)
                  
                Else
                  
                  SetInfoBarArea("Headings", "Error", "Could not allocate a buffer for the picture data", #PB_Compiler_Procedure)
                  
                EndIf
                
                CloseFile(PosterFileHandle.i)
                
              Else
                
                SetInfoBarArea("Headings", "Error", "Could not open the designated file to read from", #PB_Compiler_Procedure)
                
              EndIf
              
            Else
              
              SetInfoBarArea("Headings", "Error", "Picture filename is empty, nothing to save", #PB_Compiler_Procedure)
              
            EndIf
            
            MessageRequester("Table insert okay", "Added new movie to OMDBAPI database", #PB_MessageRequester_Ok)
            
          EndIf
          
        Else
          
          ; Debug DatabaseError()
          ; Debug "------------------------------"
          ; Debug InsertString.s
          
          MessageRequester("Database insert error", "Could not add new movie to OMDBAPI database:::  "  + DatabaseError(), #PB_MessageRequester_Ok)
          
        EndIf
        
        SetWindowTitle(#Window_OMDBAPI, Program\OldWindowTitle + " Saved current movie title to database")
        
      Else
        
        SetInfoBarArea("Headings", "Error", "Title area is blank so we will not save any details to the database", #PB_Compiler_Procedure)
        
      EndIf
      
      ;------------------------------------------------------------------------------------------
      ; 
      ;------------------------------------------------------------------------------------------

    Case  "Edit Current Movie"

      ;--------------------------------------------------------------------------------------------
      ; 
      ;--------------------------------------------------------------------------------------------
      
      If Movie\Title.s <> #Empty$ And Movie\ImdbID.s <> #Empty$  And Movie\Title.s  <> ">> Error : Movie not found! <<"   And Left(Movie\ImdbID.s, 2) = "tt"
        
        ;------------------------------------------------------------------------------------------
        ; 
        ;------------------------------------------------------------------------------------------
        
        SetDatabaseString(Program\DatabaseHandle, 24, Program\CurrentRecord)
        
        ;------------------------------------------------------------------------------------------
        ; 
        ;------------------------------------------------------------------------------------------
        
        UpdateString.s      + 

        "UPDATE db_movies " + 

        "SET title = ?, "   + 

        "year = ?, "        + 

        "rated = ?, "       + 

        "released = ?, "    + 

        "runtime = ?, "     + 

        "genre = ?, "       + 

        "director = ?, "    + 

        "writer = ?, "      + 

        "actors = ?, "      + 

        "plot = ?, "        + 

        "language = ?, "    + 

        "country = ?, "     + 

        "awards = ?, "      + 

        "poster = ?, "      + 

        "ratings = ?, "     + 

        "metascore = ?, "   + 

        "imdbrating = ?, "  + 

        "imdbvotes = ?, "   + 

        "imdbid = ?, "      + 

        "type = ?, "        + 

        "dvd = ?, "         + 

        "boxoffice = ?, "   + 

        "production = ?, "  + 

        "website = ? "      + 
        
        "WHERE recordid = ?"

        If DatabaseUpdate(Program\DatabaseHandle, UpdateString.s) <> #DatabaseUpdateFail
          
          MessageRequester("Table update okay", "Added edited movie to OMDBAPI database", #PB_MessageRequester_Ok)
          
        Else
          
          MessageRequester("Database update error", "Could not add edited movie to OMDBAPI database:::  "  + DatabaseError(), #PB_MessageRequester_Ok)
          
        EndIf
        
        SetWindowTitle(#Window_OMDBAPI, Program\OldWindowTitle + " Saved current movie title to database")
        
        ;------------------------------------------------------------------------------------------
        ; 
        ;------------------------------------------------------------------------------------------
        
      Else
        
        SetInfoBarArea("Headings", "Error", "Title area is blank so we will not save any details to the database", #PB_Compiler_Procedure)
        
      EndIf
      
      ;--------------------------------------------------------------------------------------------
      ; 
      ;--------------------------------------------------------------------------------------------
      
  EndSelect
  
  ;------------------------------------------------------------------------------------------------
  ; 
  ;------------------------------------------------------------------------------------------------
  
  ClearAllFields()
  
  ;------------------------------------------------------------------------------------------------
  ; 
  ;------------------------------------------------------------------------------------------------
  
  CloseMyWindow(#Window_Dataform)
  
  ;------------------------------------------------------------------------------------------------
  ; 
  ;------------------------------------------------------------------------------------------------
  
  ClearStructure(@Movie, MovieData)
  
  ;------------------------------------------------------------------------------------------------
  ; 
  ;------------------------------------------------------------------------------------------------
  
  SortListIconEntries()
  
  ;------------------------------------------------------------------------------------------------
  ; 
  ;------------------------------------------------------------------------------------------------
  
EndProcedure


; IDE Options = PureBasic 5.70 LTS (Windows - x86)
; CursorPosition = 12
; FirstLine = 258
; Folding = -
; EnableXP
; CompileSourceDirectory
; EnableCompileCount = 0
; EnableBuildCount = 0
; EnableExeConstant