from tqdm import tqdm

GREY  = "\033[90m"
GREEN = "\033[92m"
RESET = "\033[0m"


def loading_bars():
    bar_format_normal  = "{bar} {l_bar} {remaining} {postfix}"
    bar_format_learner = "{bar} {remaining} {postfix}"
    # YENİ: 4 bilgi (Epoch, Train Accuracy, Train Loss, Batch) tek satırda
    bar_format_learner_batch = "{bar} {remaining} {postfix}"
    return bar_format_normal, bar_format_learner, bar_format_learner_batch


def get_loading_bar_style():
    return (f"{GREY}━{RESET}", f"{GREEN}━{RESET}")


def initialize_loading_bar(total, desc, ncols, bar_format,
                           loading_bar_style=get_loading_bar_style(), leave=True):
    """Mevcut bar — değişmedi."""
    return tqdm(
        total=total,
        leave=leave,
        desc=desc,
        ascii=loading_bar_style,
        bar_format=bar_format,
        ncols=ncols,
    )


def initialize_loading_bar_batch(total, desc, ncols,
                                 loading_bar_style=get_loading_bar_style(), leave=True):
    """
    Gradient train için batch bilgisi de içeren bar.

    Postfix düzeni (tek bar, 4 alan):
        Epoch: 5/100  Train Accuracy: 0.9123  Train Loss: 0.2341  Batch: 3/8

    Kullanım (_run_gradient_train içinde):
        progress = initialize_loading_bar_batch(total=grad_epochs, desc="", ncols=100)
        postfix_dict["Epoch"]          = f"{epoch}/{grad_epochs}"
        postfix_dict["Train Accuracy"] = format_number(train_acc)
        postfix_dict["Train Loss"]     = format_number(train_loss)
        postfix_dict["Batch"]          = f"{batch_idx}/{n_batches}"
        progress.set_postfix(postfix_dict)
        progress.update(1)
    """
    _, _, bar_format_learner_batch = loading_bars()
    return tqdm(
        total=total,
        leave=leave,
        desc=desc,
        ascii=loading_bar_style,
        bar_format=bar_format_learner_batch,
        ncols=ncols,
    )