Help on making bar fill in order

My goal is to recreate fabricator from game called “deep space airship” an online game. but i stumbled across a huge issue

Lets say i wanna fill that bar with metal, bar fills metal, if i wanna fill bar with other material, it goes to other bar, if bar is full and matches with same material, and if you craft something, and lets say i wanna craft something that costs 32 metals

first bar = 16
second bar = 16

normaly i want to calculate total of those materials

here is the example

image

other example

and finaly
image

here is the place file
crafter.rbxl (98.9 KB)

help is appreciated