package cn.forevermc.launcher.view.pages;

import cn.forevermc.launcher.bean.FileDownload;
import cn.forevermc.launcher.view.components.AbstractListPane;
import cn.forevermc.launcher.view.components.control.MCButton;
import cn.forevermc.launcher.view.components.control.MCCheckBox;
import cn.forevermc.launcher.view.components.control.MCLabel;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ListCell;
import javafx.scene.control.OverrunStyle;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextAlignment;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.utils.BorderStroke;
import com.imyeyu.fx.utils.Column;
import com.imyeyu.fx.utils.NoSelectionModel;

/**
 * 资源下载面板
 *
 * @author 夜雨
 * @since 2022-11-28 19:18
 */
public abstract class ResourceDownloadPane extends AbstractListPane<FileDownload> {

	protected final MCLabel speed;
	protected final MCButton cancel;
	protected final MCCheckBox bgmToggle;
	protected final ProgressBar pb;

	public ResourceDownloadPane() {
		// 列表
		list.setSelectionModel(new NoSelectionModel<>());
		list.setCellFactory(cell -> new ListCell<>() {

			private final MCLabel text;
			private final BorderPane root;
			private final ProgressBar pb;

			{
				text = new MCLabel();
				text.setAlignment(Pos.CENTER_RIGHT);
				text.setTextOverrun(OverrunStyle.LEADING_ELLIPSIS);
				text.setTextAlignment(TextAlignment.RIGHT);
				pb = new ProgressBar();
				pb.setPrefHeight(20);
				root = new BorderPane();
				root.setCenter(text);
				root.setRight(pb);

				text.maxWidthProperty().bind(root.widthProperty().subtract(pb.widthProperty()).subtract(40));
			}

			@Override
			protected void updateItem(FileDownload item, boolean empty) {
				super.updateItem(item, empty);
				if (empty || item == null) {
					setGraphic(null);
				} else {
					text.setText(item.getDisplayName());
					if (text.textFillProperty().isBound()) {
						text.textFillProperty().unbind();
					}
					text.textFillProperty().bind(Bindings.when(item.errorProperty()).then(Colorful.RED).otherwise(Colorful.WHITE));

					if (pb.progressProperty().isBound()) {
						pb.progressProperty().unbind();
					}
					pb.progressProperty().bind(item.progressProperty());
					setGraphic(root);
				}
			}
		});

		// 总进度
		MCLabel labelPB = new MCLabel(TimiFXUI.MULTILINGUAL.text("fmc.launcher.resource_download.total"));
		labelPB.setTextFill(Colorful.WHITE);

		speed = new MCLabel();

		pb = new ProgressBar();
		pb.setMaxWidth(Double.MAX_VALUE);
		pb.setPrefHeight(28);

		// 取消
		cancel = new MCButton(TimiFXUI.MULTILINGUAL.text("cancel"));
		cancel.getLabel().setPadding(new Insets(8, 24, 8, 24));

		// 背景音乐
		bgmToggle = new MCCheckBox();
		bgmToggle.setSelected(true);
		bgmToggle.managedProperty().bind(bgmToggle.visibleProperty());

		setTitle(TimiFXUI.MULTILINGUAL.text("fmc.launcher.resource_download"));
		setBottomPaneValue(new BorderPane() {{
			setMargin(cancel, new Insets(0, 0, 0, 16));
			setAlignment(labelPB, Pos.CENTER_RIGHT);
			prefWidthProperty().bind(bottom.widthProperty());
			setLeft(labelPB);
			setCenter(new StackPane(pb, speed));
			setRight(new HBox() {{
				setMargin(bgmToggle, new Insets(0, 0, 0, 16));
				setSpacing(4);
				setPadding(new Insets(0, 0, 0, 12));
				setAlignment(Pos.CENTER);
				getChildren().addAll(cancel, new HBox() {{
					setSpacing(4);
					setAlignment(Pos.CENTER);
					visibleProperty().bind(bgmToggle.visibleProperty());
					managedProperty().bind(visibleProperty());
					getChildren().addAll(bgmToggle, new MCLabel() {{
						bgmToggle.setLabel(this);
						setText(TimiFXUI.MULTILINGUAL.text("bgm"));
					}});
				}});
			}});
		}});
	}
}
