# Copyright (C) 2021  Sven J. Grewe and other contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

from flbry.variables import clr
from flbry import variables
import shutil

terminal_width = shutil.get_terminal_size((80,40)).columns # Get terminal_width. Default to 40 if there is no info.
empty_side = terminal_width//20 # Five percent on each verticle side.

# Box colours (look at 'variables.clr' in the 'flbry' folder for more information):
box_margin_clr = clr["bdma"] # Box margin colour
box_inside_clr = clr["bdbu"] # Colour inside the box

def midtext(txt = "",txtposition = "mid",box = "inside"): # Set the standard parameter for this function.
    box_left_side = " "*empty_side+box_margin_clr+" "+box_inside_clr+clr["bold"]
    box_right_side = box_margin_clr+" "+clr["norm"]
    
    add_l = " "*((terminal_width-(empty_side*2)-len(txt))//2) # Space between margin and text
    add_r = " "*(terminal_width-(empty_side*2)-len(txt)-len(add_l)) # Space between text and margin
    
    if box == "horizontal_side":
        complete = box_left_side+box_margin_clr+add_l+txt+add_r+box_right_side
    elif box == "inside":
        complete = box_left_side+add_l+txt+add_r+box_right_side
    elif box == None:
        complete = " "*((terminal_width-len(txt))//2)+txt
    else:
        complete = txt
    return complete # Complete line to print
