-- Addon: WoWTR Cataclysm-Books (version: 4.00) 2024/05/03
-- Description: AddOn displays translated books.
-- Autor: Platine  (e-mail: platine.wow@gmail.com)
BT_Interface = {     -- teksty w Interface / Addons - ustawienia programu
   active            = "Aktif",
   title             = "Kitap başlığının çevirisini göster",
   showID            = "Kitap kimliğini göster",
   saveNW            = "Çevrilmemiş verileri kaydet",
};

BT_Messages = {      -- komunikaty programu w Chat
   loaded            = "başlatıldı",
   is_now_active     = "şimdi aktif",
   is_now_not_active = "şimdi pasif",
};
-- General Variables
local BK_version = GetAddOnMetadata("WoWTR_Cataclysm", "Version");
-- fonty z polskimi znakami diakrytycznymi (także innych narodów)
local BT_Font = QTR_Font2;
local BT_act_tr = "0";
local BT_bookID = "0";
local BT_tytul_en, BT_tytul_pl, BT_tekst_en, BT_tekst_pl = "";


function BookTranslator_ShowTranslation()
   if (BT_PM["active"] == "1") then
      BT_ToggleButton0:Disable();		
      BT_ToggleButton0:SetWidth(40);
      BT_act_tr = "0";
      BT_tytul_en=ItemTextGetItem();
      BT_tekst_en=ItemTextGetText();
      BT_nr_str=tostring(ItemTextGetPage());
      local par1, par2, par3 = GetItemInfo(ItemTextGetItem());
      if (par2) then
         local pa1, itemID, pa3 = strsplit(":",par2);
         BT_bookID = itemID;
      else
         local BT_znacznik=BT_tytul_en.."#"..BT_nr_str.."#"..string.sub(BT_tekst_en,1,15);
         if (BT_BooksID[BT_znacznik]) then		          -- jest znacznik w bazie ID - pobierz bookID
            BT_bookID = BT_BooksID[BT_znacznik];          -- jako string
         end
      end
      if (BT_bookID) then
         if (BT_Books[BT_bookID]) then	                -- jest tłumaczenie tej książki
            if (BT_Books[BT_bookID][BT_nr_str]) then   	 -- jest tłumaczenie tej strony
               if (BT_PM["title"] == "1") then			    -- wyświetlaj tłumaczenie tytułu
                  BT_tytul_pl=PolStd(BT_Books[BT_bookID]["Title"]);
                  BT_ToggleButton1:SetText(BT_tytul_pl);
                  BT_ToggleButton1:Show();
               end
               BT_tekst_pl=string.gsub(BT_Books[BT_bookID][BT_nr_str],"$B","\n");
               ItemTextPageText:SetText(BT_tekst_pl);
               if (BT_PM["setsize"]=="1") then
                  ItemTextPageText:SetFont(BT_Font, BT_PM["fontsize"]);
               else
                  ItemTextPageText:SetFont(BT_Font, 13);
               end
               if (BT_PM["showID"]=="1") then				 -- pokaż ID książki
                  BT_ToggleButton0:SetText("Book ID: "..BT_bookID.." (PL)");
                  BT_ToggleButton0:SetWidth(150);
               else
                  BT_ToggleButton0:SetText("PL");
               end
               BT_ToggleButton0:Enable();
               BT_act_tr = "1";		                      -- aktualnie jest wyświetlane polskie tłumaczenie
            else	                                        -- brak tłumaczenia
               if (BT_PM["showID"] == "1") then				 -- pokaż ID książki
                  BT_ToggleButton0:SetText("Book ID: "..BT_bookID.." (EN)");
                  BT_ToggleButton0:SetWidth(150);
	            end  
               BT_save_original();
            end
         else
            BT_save_original();       -- jest ID pisma,listu, ale nie ma tłumaczenia - zapisz je
         end
      end
   else	 
      BT_ToggleButton0:Hide();      -- okienko ze znacznikiem EN lub PL
   end
end 


function BT_save_original()
   if (BT_PM["saveNW"] == "1") then              -- zapisz tekst angielski do pliku
      if (strlen(BT_nr_str)==1) then
	     BT_nr_str="0"..BT_nr_str;
      end
      BT_SAVED[BT_bookID.." STR"..BT_nr_str]=BT_tytul_en.."@"..BT_tekst_en;
      --DEFAULT_CHAT_FRAME:AddMessage("|cffffff00Kitap ID: "..BT_bookID);
   end
end





function BookTranslator_SlashCommand(msg)
  -- check the command
  if (msg) then
     local BT_command = string.lower(msg);                -- normalizacja, tylko małe litery
     if ((BT_command=="on") or (BT_command=="1")) then    -- włącz przełącznik aktywności
        BT_PM["active"]="1";
        DEFAULT_CHAT_FRAME:AddMessage("|cffffff00WoWTR Cataclysm-Books "..BT_Messages.is_now_active);
     elseif ((BT_command=="off") or (BT_command=="0")) then
        BT_PM["active"]="0";
        DEFAULT_CHAT_FRAME:AddMessage("|cffffff00WoWTR Cataclysm-Books "..BT_Messages.is_now_not_active);
     else
  	     InterfaceOptionsFrame_Show();
  	     InterfaceOptionsFrame_OpenToCategory("WoWTR Cataclysm-Books");
     end   
  end
end


function BookTranslator_CheckVars()
  if (not BT_PM) then
     BT_PM = {};
  end
  if (not BT_SAVED) then
     BT_SAVED = {};
  end
  -- initialize check options
  if (not BT_PM["active"] ) then    -- dodatek aktywny
     BT_PM["active"] = "1";   
  end
  if (not BT_PM["title"] ) then     -- pokaż tłumaczenie tytułu
     BT_PM["title"] = "1";   
  end
--  if (not BT_PM["showID"] ) then    -- pokaż ID książki
--     BT_PM["showID"] = "0";   
--  end
  if (not BT_PM["setsize"] ) then   -- uaktywnij zmiany wielkości czcionki
     BT_PM["setsize"] = "1";   
  end
  if (not BT_PM["fontsize"] ) then  -- wielkość czcionki
     BT_PM["fontsize"] = "15";   
  end
  if (not BT_PM["saveNW"] ) then    -- zapisz nieprzetłumaczone
     BT_PM["saveNW"] = "1";   
  end
end
  
function BookTranslator_SetCheckButtonState()
  BookTranslatorCheckButton0:SetChecked(BT_PM["active"]=="1");
  BookTranslatorCheckButton1:SetChecked(BT_PM["title"]=="1");
  BookTranslatorCheckButton3:SetChecked(BT_PM["showID"]=="1");
  BookTranslatorCheckButton5:SetChecked(BT_PM["saveNW"]=="1");
end


function BookTranslator_BlizzardOptions()

-- Create main frame for information text
local BookTranslatorOptions = CreateFrame("FRAME", "BookTranslatorOptions");
--BookTranslatorOptions:SetScript("OnShow", function(self) BookTranslator_SetCheckButtonState() end);
BookTranslatorOptions.name = "WoWTR Cataclysm-Books";
BookTranslatorOptions.refresh = function (self) BookTranslator_SetCheckButtonState() end;
BookTranslatorOptions:SetScript("OnShow", function (self) BookTranslator_SetCheckButtonState() end);

  -- Yeni ekleme yöntemi
  if Settings and Settings.RegisterCanvasLayoutCategory then
    local Category = Settings.RegisterCanvasLayoutCategory(BookTranslatorOptions, "WoWTR Cataclysm-Books")
    Settings.RegisterAddOnCategory(Category)
  elseif InterfaceOptions_AddCategory then
    InterfaceOptions_AddCategory(BookTranslatorOptions);
  end

local BookTranslatorOptionsHeader = BookTranslatorOptions:CreateFontString(nil, "ARTWORK");
BookTranslatorOptionsHeader:SetFontObject(GameFontNormalLarge);
BookTranslatorOptionsHeader:SetJustifyH("LEFT"); 
BookTranslatorOptionsHeader:SetJustifyV("TOP");
BookTranslatorOptionsHeader:ClearAllPoints();
BookTranslatorOptionsHeader:SetPoint("TOPLEFT", 16, -16);
BookTranslatorOptionsHeader:SetText("WoWTR Cataclysm Books, ver. "..BK_version.." by Platine, hknylmz © 2024");

local BookTranslatorCheckButton0 = CreateFrame("CheckButton", "BookTranslatorCheckButton0", BookTranslatorOptions, "UICheckButtonTemplate");
BookTranslatorCheckButton0:SetPoint("TOPLEFT", BookTranslatorOptionsHeader, "BOTTOMLEFT", 0, -40);
BookTranslatorCheckButton0:SetScript("OnClick", function(self) if (BT_PM["active"]=="1") then BT_PM["active"]="0" else BT_PM["active"]="1" end; end);
BookTranslatorCheckButton0Text:SetFont(BT_Font, 13);
BookTranslatorCheckButton0Text:SetText(BT_Interface.active);     -- dodatek aktywny

local BookTranslatorCheckButton1 = CreateFrame("CheckButton", "BookTranslatorCheckButton1", BookTranslatorOptions, "UICheckButtonTemplate");
BookTranslatorCheckButton1:SetPoint("TOPLEFT", BookTranslatorCheckButton0, "BOTTOMLEFT", 20, -10);
BookTranslatorCheckButton1:SetScript("OnClick", function(self) if (BT_PM["title"]=="1") then BT_PM["title"]="0" else BT_PM["title"]="1" end; end);
BookTranslatorCheckButton1Text:SetFont(BT_Font, 13);
BookTranslatorCheckButton1Text:SetText(BT_Interface.title);      -- tłumacz tytuł

local BookTranslatorCheckButton3 = CreateFrame("CheckButton", "BookTranslatorCheckButton3", BookTranslatorOptions, "UICheckButtonTemplate");
BookTranslatorCheckButton3:SetPoint("TOPLEFT", BookTranslatorCheckButton1, "BOTTOMLEFT", 0, -10);
BookTranslatorCheckButton3:SetScript("OnClick", function(self) if (BT_PM["showID"]=="1") then BT_PM["showID"]="0" else BT_PM["showID"]="1"; end; end);
BookTranslatorCheckButton3Text:SetFont(BT_Font, 13);
BookTranslatorCheckButton3Text:SetText(BT_Interface.showID);     -- pokaż nr ID książki

local BookTranslatorCheckButton5 = CreateFrame("CheckButton", "BookTranslatorCheckButton5", BookTranslatorOptions, "UICheckButtonTemplate");
BookTranslatorCheckButton5:SetPoint("TOPLEFT", BookTranslatorCheckButton3, "BOTTOMLEFT", 0, -10);
BookTranslatorCheckButton5:SetScript("OnClick", function(self) if (BT_PM["saveNW"]=="1") then BT_PM["saveNW"]="0" else BT_PM["saveNW"]="1" end; end);
BookTranslatorCheckButton5Text:SetFont(BT_Font, 13);
BookTranslatorCheckButton5Text:SetText(BT_Interface.saveNW);     -- zapisz nieprzetłumaczone książki




  local BKDISCORD = BookTranslatorOptions:CreateFontString(nil, "ARTWORK");
  BKDISCORD:SetFontObject(GameFontWhite);
  BKDISCORD:SetJustifyH("LEFT");
  BKDISCORD:SetJustifyV("TOP");
  BKDISCORD:ClearAllPoints();
  BKDISCORD:SetPoint("TOPLEFT", BookTranslatorOptionsHeader, "BOTTOMLEFT", 0, -450);
  BKDISCORD:SetFont(QTR_Font2, 13);
  BKDISCORD:SetText("Discord Adresimiz:");
  
  local BKDISCORD2 = CreateFrame("EditBox", "BKDISCORD2", BookTranslatorOptions, "InputBoxTemplate");
  BKDISCORD2:ClearAllPoints();
  BKDISCORD2:SetPoint("TOPLEFT", BKDISCORD, "LEFT", 5, -10);
  BKDISCORD2:SetHeight(20);
  BKDISCORD2:SetWidth(210);
  BKDISCORD2:SetAutoFocus(false);
  BKDISCORD2:SetFontObject(GameFontGreen);
  BKDISCORD2:SetText("https://discord.gg/PEknRSqEQ7");
  BKDISCORD2:SetCursorPosition(0);
  BKDISCORD2:SetScript("OnEnter", function(self)
	  GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT")
      getglobal("GameTooltipTextLeft1"):SetFont(QTR_Font2, 13);
  	  GameTooltip:SetText(QTR_Reklama.WWW3, nil, nil, nil, nil, true)
	  GameTooltip:Show() --Show the tooltip
     end);
  BKDISCORD2:SetScript("OnLeave", function(self)
      getglobal("GameTooltipTextLeft1"):SetFont(Original_Font2, 13);
	  GameTooltip:Hide() --Hide the tooltip
     end);
  BKDISCORD2:SetScript("OnTextChanged", function(self) BKDISCORD2:SetText("https://discord.gg/PEknRSqEQ7"); end);


  local BOOKWWW1 = BookTranslatorOptions:CreateFontString(nil, "ARTWORK");
  BOOKWWW1:SetFontObject(GameFontWhite);
  BOOKWWW1:SetJustifyH("LEFT");
  BOOKWWW1:SetJustifyV("TOP");
  BOOKWWW1:ClearAllPoints();
  BOOKWWW1:SetPoint("TOPLEFT", BKDISCORD, "BOTTOMLEFT", 0, -40);
  BOOKWWW1:SetFont(QTR_Font2, 13);
  BOOKWWW1:SetText("WoWTR Web Sayfamız:");
  
local BKWWW2 = CreateFrame("EditBox", "BKWWW2", BookTranslatorOptions, "InputBoxTemplate");
BKWWW2:ClearAllPoints();
BKWWW2:SetPoint("TOPLEFT", BOOKWWW1, "LEFT", 5, -10);
BKWWW2:SetHeight(20);
BKWWW2:SetWidth(210);
BKWWW2:SetAutoFocus(false);
BKWWW2:SetFontObject(GameFontGreen);
BKWWW2:SetText("https://www.wowtr.com.tr");
BKWWW2:SetCursorPosition(0);
BKWWW2:SetScript("OnEnter", function(self)
   GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT")
   getglobal("GameTooltipTextLeft1"):SetFont(BB_Font, 13);
   GameTooltip:SetText("tıklayın ve panoya kopyalamak için Ctrl+C tuşlarına basın", nil, nil, nil, nil, true)
   GameTooltip:Show() --Show the tooltip
   end);
BKWWW2:SetScript("OnLeave", function(self)
   GameTooltip:Hide() --Hide the tooltip
   end);
BKWWW2:SetScript("OnTextChanged", function(self) BKWWW2:SetText("https://www.wowtr.com.tr"); end);
end

function PolStd(txt)
   txt = string.gsub(txt, "ą", "a");
   txt = string.gsub(txt, "ć", "c");
   txt = string.gsub(txt, "ę", "e");
   txt = string.gsub(txt, "ń", "n");
   txt = string.gsub(txt, "ł", "l");
   txt = string.gsub(txt, "ó", "o");
   txt = string.gsub(txt, "ś", "s");
   txt = string.gsub(txt, "ź", "z");
   txt = string.gsub(txt, "ż", "z");
   txt = string.gsub(txt, "Ą", "A");
   txt = string.gsub(txt, "Ć", "C");
   txt = string.gsub(txt, "Ę", "E");
   txt = string.gsub(txt, "Ń", "N");
   txt = string.gsub(txt, "Ł", "L");
   txt = string.gsub(txt, "Ó", "O");
   txt = string.gsub(txt, "Ś", "S");
   txt = string.gsub(txt, "Ź", "Z");
   txt = string.gsub(txt, "Ż", "Z");
   return txt;
end

ItemTextFrame:HookScript("OnShow", function() BookTranslator_ShowTranslation() end);
ItemTextNextPageButton:HookScript("OnClick", function() BookTranslator_ShowTranslation() end);
ItemTextPrevPageButton:HookScript("OnClick", function() BookTranslator_ShowTranslation() end);

BookTranslator_CheckVars();
BookTranslator_BlizzardOptions();

BT_ToggleButton0 = CreateFrame("Button",nil, ItemTextFrame, "UIPanelButtonTemplate");


BT_ToggleButton1 = CreateFrame("Button",nil, ItemTextFrame, "UIPanelButtonTemplate");
BT_ToggleButton1:SetWidth(270);
BT_ToggleButton1:SetHeight(20);
BT_ToggleButton1:SetText("");
--BT_ToggleButton1:SetFont(BT_Font, 13);
BT_ToggleButton1:Hide();
BT_ToggleButton1:ClearAllPoints();
BT_ToggleButton1:SetPoint("TOPLEFT", ItemTextFrame, "TOPLEFT", 65, -15);
