001package myhw3.main;
002
003import myhw3.command.Command;
004import myhw3.data.Data;
005import myhw3.data.Inventory;
006import myhw3.data.Video;
007import myhw3.ui.UI;
008import myhw3.ui.UIError;
009import myhw3.ui.UIForm;
010import myhw3.ui.UIFormBuilder;
011import myhw3.ui.UIFormTest;
012import myhw3.ui.UIMenu;
013import myhw3.ui.UIMenuBuilder;
014
015class Control {
016        private static final int EXITED = 0;
017        private static final int EXIT = 1;
018        private static final int START = 2;
019        private static final int NUMSTATES = 10;
020        private UIMenu[] menus;
021        private int state;
022
023        private UIForm getVideoForm;
024        private UIFormTest numberTest;
025        private UIFormTest stringTest;
026
027        private Inventory inventory;
028        private UI ui;
029
030        Control(Inventory inventory, UI ui) {
031                this.inventory = inventory;
032                this.ui = ui;
033
034                menus = new UIMenu[NUMSTATES];
035                state = START;
036                addSTART(START);
037                addEXIT(EXIT);
038
039                UIFormTest yearTest = input -> {
040                        try {
041                                int i = Integer.parseInt(input);
042                                return i > 1800 && i < 5000;
043                        } catch (NumberFormatException e) {
044                                return false;
045                        }
046                };
047                numberTest = input -> {
048                        try {
049                                Integer.parseInt(input);
050                                return true;
051                        } catch (NumberFormatException e) {
052                                return false;
053                        }
054                };
055                stringTest = input -> ! "".equals(input.trim());
056
057                UIFormBuilder f = new UIFormBuilder();
058                f.add("Title", stringTest);
059                f.add("Year", yearTest);
060                f.add("Director", stringTest);
061                getVideoForm = f.toUIForm("Enter Video");
062        }
063
064        void run() {
065                try {
066                        while (state != EXITED) {
067                                ui.processMenu(menus[state]);
068                        }
069                } catch (UIError e) {
070                        ui.displayError("UI closed");
071                }
072        }
073
074        private void addSTART(int stateNum) {
075                UIMenuBuilder m = new UIMenuBuilder();
076
077                m.add("Default",
078                                () -> ui.displayError("doh!"));
079                m.add("Add/Remove copies of a video",
080                                () -> {
081                                        String[] result1 = ui.processForm(getVideoForm);
082                                        Video v = Data.newVideo(result1[0], Integer.parseInt(result1[1]), result1[2]);
083
084                                        UIFormBuilder f = new UIFormBuilder();
085                                        f.add("Number of copies to add/remove", numberTest);
086                                        String[] result2 = ui.processForm(f.toUIForm(""));
087
088                                        Command c = Data.newAddCmd(inventory, v, Integer.parseInt(result2[0]));
089                                        if (! c.run()) {
090                                                ui.displayError("Command failed");
091                                        }
092                                });
093                m.add("Check in a video",
094                                () -> {
095                                        // TODO
096                                });
097                m.add("Check out a video",
098                                () -> {
099                                        // TODO
100                                });
101                m.add("Print the inventory",
102                                () -> ui.displayMessage(inventory.toString()));
103                m.add("Clear the inventory",
104                                () -> {
105                                        if (!Data.newClearCmd(inventory).run()) {
106                                                ui.displayError("Command failed");
107                                        }
108                                });
109                m.add("Undo",
110                                () -> {
111                                        if (!inventory.undo()) {
112                                                ui.displayError("Command failed");
113                                        }
114                                });
115                m.add("Redo",
116                                () -> {
117                                        if (!inventory.redo()) {
118                                                ui.displayError("Command failed");
119                                        }
120                                });
121                m.add("Print top ten all time rentals in order",
122                                () -> {
123                                        // TODO
124                                });
125
126                m.add("Exit",
127                                () -> state = EXIT);
128
129                m.add("Initialize with bogus contents",
130                                () -> {
131                                        Data.newAddCmd(inventory, Data.newVideo("a", 2000, "m"), 1).run();
132                                        Data.newAddCmd(inventory, Data.newVideo("b", 2000, "m"), 2).run();
133                                        Data.newAddCmd(inventory, Data.newVideo("c", 2000, "m"), 3).run();
134                                        Data.newAddCmd(inventory, Data.newVideo("d", 2000, "m"), 4).run();
135                                        Data.newAddCmd(inventory, Data.newVideo("e", 2000, "m"), 5).run();
136                                        Data.newAddCmd(inventory, Data.newVideo("f", 2000, "m"), 6).run();
137                                        Data.newAddCmd(inventory, Data.newVideo("g", 2000, "m"), 7).run();
138                                        Data.newAddCmd(inventory, Data.newVideo("h", 2000, "m"), 8).run();
139                                        Data.newAddCmd(inventory, Data.newVideo("i", 2000, "m"), 9).run();
140                                        Data.newAddCmd(inventory, Data.newVideo("j", 2000, "m"), 10).run();
141                                        Data.newAddCmd(inventory, Data.newVideo("k", 2000, "m"), 11).run();
142                                        Data.newAddCmd(inventory, Data.newVideo("l", 2000, "m"), 12).run();
143                                        Data.newAddCmd(inventory, Data.newVideo("m", 2000, "m"), 13).run();
144                                        Data.newAddCmd(inventory, Data.newVideo("n", 2000, "m"), 14).run();
145                                        Data.newAddCmd(inventory, Data.newVideo("o", 2000, "m"), 15).run();
146                                        Data.newAddCmd(inventory, Data.newVideo("p", 2000, "m"), 16).run();
147                                        Data.newAddCmd(inventory, Data.newVideo("q", 2000, "m"), 17).run();
148                                        Data.newAddCmd(inventory, Data.newVideo("r", 2000, "m"), 18).run();
149                                        Data.newAddCmd(inventory, Data.newVideo("s", 2000, "m"), 19).run();
150                                        Data.newAddCmd(inventory, Data.newVideo("t", 2000, "m"), 20).run();
151                                        Data.newAddCmd(inventory, Data.newVideo("u", 2000, "m"), 21).run();
152                                        Data.newAddCmd(inventory, Data.newVideo("v", 2000, "m"), 22).run();
153                                        Data.newAddCmd(inventory, Data.newVideo("w", 2000, "m"), 23).run();
154                                        Data.newAddCmd(inventory, Data.newVideo("x", 2000, "m"), 24).run();
155                                        Data.newAddCmd(inventory, Data.newVideo("y", 2000, "m"), 25).run();
156                                        Data.newAddCmd(inventory, Data.newVideo("z", 2000, "m"), 26).run();
157                                });
158
159                menus[stateNum] = m.toUIMenu("Bob's Video");
160        }
161        private void addEXIT(int stateNum) {
162                UIMenuBuilder m = new UIMenuBuilder();
163
164                m.add("Default", () -> {});
165                m.add("Yes",
166                                () -> state = EXITED);
167                m.add("No",
168                                () -> state = START);
169
170                menus[stateNum] = m.toUIMenu("Are you sure you want to exit?");
171        }
172}