Files @ 46b3b97a9901
Branch filter:

Location: ATITD-Tools/Desert-Paint-Lab/UI/RecipeGeneratorView.cs - annotation

jmaltzen
Don't allow <=0 screen resolution or pixel scale settings, as that doesn't make sense.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
66a2d6c441ac
66a2d6c441ac
66a2d6c441ac
66a2d6c441ac
66a2d6c441ac
66a2d6c441ac
66a2d6c441ac
66a2d6c441ac
66a2d6c441ac
66a2d6c441ac
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
50bbf5233575
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
/*
 * Copyright (c) 2015, Jason Maltzen

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Concurrent;

namespace DesertPaintLab
{
    [System.ComponentModel.ToolboxItem(true)]
    public partial class RecipeGeneratorView : Gtk.Bin
    {
        RecipeGenerator generator;
        PlayerProfile profile;
        bool canceling = false;
        bool running = false;
        bool pauseForCheckpoint = false;

        const long RECIPE_SAVE_INTERVAL = 30000; // msec between saving recipes
        const long CHECKPOINT_INTERVAL = 17000; // msec between saving out generator state
        const string PAINT_STATE_FILE = "dp_generator_state";
        const string RIBBON_STATE_FILE = "dp_generator_ribbon_state";

        static Gtk.ListStore colorStore = new Gtk.ListStore(typeof(string));

        List<KeyValuePair<string, string>> missingReactions = new List<KeyValuePair<string, string>>();

        long lastProgressUpdate;
        long lastStatusUpdate;
        long lastProfileSave;
        long lastCheckpoint;

        public EventHandler SetStatus;
        public EventHandler Started;
        public EventHandler Stopped;

        static public Gtk.ListStore RecipeModel
        {
            get
            {
                return colorStore;   
            }
        }

        Gtk.ThreadNotify notifyFinished;
        Gtk.ThreadNotify notifyProgress;
        Gtk.ThreadNotify notifyNewRecipe;

        ConcurrentQueue<PaintRecipe> pendingNewRecipes = new ConcurrentQueue<PaintRecipe>();

        Gtk.ListStore reagentListStore;
        // end reagent view

        public RecipeGeneratorView(PlayerProfile profile) : base()
        {
            this.profile = profile;
            this.Build();
            minIngredientsSpinButton.Value = 1; // TODO: read/save profile info
            maxIngredientsSpinButton.Value = 5; // TODO: read/save profile info
            maxRecipeSpinButton.Value = 20; // TODO: read/save profile info
            fullQuantitySpinButton.Value = 20; // TODO: read/save profile info
            fullQuantityDepthSpinButton.Value = 4; // TODO: read/save profile info

            fullQuantityDepthSpinButton.SetRange(0, ReagentManager.Names.Count);
            maxIngredientsSpinButton.SetRange(1, ReagentManager.Names.Count);
            minIngredientsSpinButton.SetRange(1, ReagentManager.Names.Count);


            Gtk.TreeViewColumn recipeColorColumn = new Gtk.TreeViewColumn();
            Gtk.CellRendererText recipeColumnCell = new Gtk.CellRendererText();
            recipeColorColumn.PackStart(recipeColumnCell, true);       
            recipeColorColumn.Title = "Color";

            recipeList.AppendColumn(recipeColorColumn);
            recipeColorColumn.AddAttribute(recipeColumnCell, "text", 0);

            colorStore.Clear();

            colorStore.SetSortColumnId(0, Gtk.SortType.Ascending);
            recipeList.Model = RecipeModel;

            recipeList.Selection.Changed += OnColorSelected;

            recipeIngredientsView.AppendColumn("Quantity", new Gtk.CellRendererText(), "text", 0);
            recipeIngredientsView.AppendColumn("Ingredient", new Gtk.CellRendererText(), "text", 1);
            recipeIngredientsView.Model = new Gtk.ListStore(typeof(string), typeof(string));

            profile.LoadRecipes();

            canceling = false;
            running = false;
            pauseForCheckpoint = false;

            generator = new RecipeGenerator(profile.Reactions);
            int threads;
            DesertPaintLab.AppSettings.Get("GeneratorThreads", out threads);
            if (threads <= 0) { threads = 15; }
            generator.MaxThreads = (uint)threads;
            generator.InitRecipes(profile.Recipes);

            generator.Progress += OnProgress;
            generator.Finished += OnFinished;
            generator.NewRecipe += OnNewRecipe;

            string stateFile = System.IO.Path.Combine(profile.Directory, PAINT_STATE_FILE);
            if (System.IO.File.Exists(stateFile))
            {
                generator.LoadState(stateFile);
                if (generator.CanResume)
                {
                    beginButton.Label = "Restart";
                    stopResumeButton.Label = "Resume";
                    stopResumeButton.Sensitive = true;

                    maxRecipeSpinButton.Value = Math.Max(generator.MaxConcentration, 14); //
                    fullQuantitySpinButton.Value = generator.FullQuantity; // TODO: read/save profile info
                    fullQuantityDepthSpinButton.Value = generator.FullQuantityDepth; // TODO: read/save profile info
                    maxIngredientsSpinButton.Value = generator.MaxReagents;
                    checkButtonRibbon.Active = false;
                }
            }
            //generator.Log = System.IO.Path.Combine(profile.Directory, "dp_log.txt");
            countLabel.Text = String.Format("{0} / {1}", profile.RecipeCount, Palette.Count);

            Destroyed += OnDestroyed;

            notifyFinished = new Gtk.ThreadNotify(new Gtk.ReadyEvent(HandleFinished));
            notifyProgress = new Gtk.ThreadNotify(new Gtk.ReadyEvent(HandleProgress));
            notifyNewRecipe = new Gtk.ThreadNotify(new Gtk.ReadyEvent(HandleNewRecipe));

            // initialize reagent list
 
            // Add the columns to the TreeView
            Gtk.TreeViewColumn reagentEnabledColumn = new Gtk.TreeViewColumn ();
            reagentEnabledColumn.Title = "Enabled";
            Gtk.CellRendererToggle reagentEnabledCell = new Gtk.CellRendererToggle ();
            reagentEnabledCell.Activatable = true;
            reagentEnabledCell.Sensitive = true;
            reagentEnabledCell.Mode = Gtk.CellRendererMode.Activatable;
            reagentEnabledCell.Visible = true;
            reagentEnabledCell.Toggled += new Gtk.ToggledHandler(OnReagentEnableToggled);
            reagentEnabledColumn.PackStart (reagentEnabledCell, true);
            //reagentEnabledColumn.AddAttribute(reagentEnabledCell, "active", 0);

            Gtk.TreeViewColumn reagentNameColumn = new Gtk.TreeViewColumn ();
            reagentNameColumn.Title = "Ingredient";
            Gtk.CellRendererText reagentNameCell = new Gtk.CellRendererText ();
            reagentNameCell.Mode = Gtk.CellRendererMode.Inert;
            reagentNameColumn.PackStart (reagentNameCell, true);
            reagentNameColumn.AddAttribute(reagentNameCell, "text", 1);
            reagentNameColumn.Expand = true;

            Gtk.TreeViewColumn reagentCostColumn = new Gtk.TreeViewColumn ();
            reagentCostColumn.Title = "Cost";
            Gtk.CellRendererText reagentCostCell = new Gtk.CellRendererText ();
            reagentCostCell.Edited += OnReagentCostChanged;
            reagentCostCell.Editable = true;
            reagentCostCell.Sensitive = true;
            reagentCostCell.Mode = Gtk.CellRendererMode.Editable;
            reagentCostColumn.PackStart (reagentCostCell, true);
            //reagentCostColumn.AddAttribute(reagentCostCell, "text", 0);

            Gtk.TreeViewColumn reagentMaxColumn = new Gtk.TreeViewColumn ();
            reagentMaxColumn.Title = "Max";
            Gtk.CellRendererText reagentMaxCell = new Gtk.CellRendererText ();
            reagentMaxCell.Edited += OnReagentQuantityChanged;
            reagentMaxCell.Editable = true;
            reagentMaxCell.Sensitive = true;
            reagentCostCell.Mode = Gtk.CellRendererMode.Editable;
            reagentMaxColumn.PackStart (reagentMaxCell, true);
            //reagentMaxColumn.AddAttribute(reagentMaxCell, "text", 0);

            reagentListStore = new Gtk.ListStore(typeof(Reagent), typeof(string), typeof(Reagent), typeof(Reagent));
            foreach (string reagentName in ReagentManager.Names)
            {
                Reagent reagent = ReagentManager.GetReagent(reagentName);
                reagentListStore.AppendValues(reagent, reagentName); // , reagent, reagent);
            }

            reagentEnabledColumn.SetCellDataFunc (reagentEnabledCell, new Gtk.TreeCellDataFunc (RenderReagentToggle));
            reagentCostColumn.SetCellDataFunc (reagentCostCell, new Gtk.TreeCellDataFunc (RenderReagentCost));
            reagentMaxColumn.SetCellDataFunc (reagentMaxCell, new Gtk.TreeCellDataFunc (RenderReagentQuantity));

            // Assign the model to the TreeView
            reagentListView.Model = reagentListStore;
            reagentListView.Sensitive = true;

            reagentListView.AppendColumn(reagentEnabledColumn);
            reagentListView.AppendColumn(reagentNameColumn);
            reagentListView.AppendColumn(reagentCostColumn);
            reagentListView.AppendColumn(reagentMaxColumn);

            bool ribbons = false;
            profile.ProfileSettings.Get("Generator.Ribbons", out ribbons);
            if (ribbons)
            {
                checkButtonRibbon.Active = true;
                InitStateForRibbons();
            }
            else
            {
                checkButtonRibbon.Active = false;
                InitStateForPaint();
            }
            
            ShowAll();
        }

        private void InitStateForPaint()
        {
            maxRecipeSpinButton.Adjustment.Lower = PaintRecipe.PAINT_RECIPE_MIN_CONCENTRATION;
            fullQuantitySpinButton.Adjustment.Upper = 30;
            if (generator == null)
            {
                generator = new RecipeGenerator(profile.Reactions);
                int threads;
                DesertPaintLab.AppSettings.Get("GeneratorThreads", out threads);
                if (threads <= 0) { threads = 15; }
                generator.MaxThreads = (uint)threads;

                generator.Progress += OnProgress;
                generator.Finished += OnFinished;
                generator.NewRecipe += OnNewRecipe;

                bool enableDebugMenu = false;
                DesertPaintLab.AppSettings.Get("EnableDebugMenu", out enableDebugMenu);
                bool logGenerator = false;
                DesertPaintLab.AppSettings.Get("GeneratorLog", out logGenerator);
                if (enableDebugMenu && logGenerator)
                {
                    string logDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    generator.Log = System.IO.Path.Combine(logDir, "dpl_generator.txt");
                }
            }
            generator.InitRecipes(profile.Recipes);

            string stateFile = System.IO.Path.Combine(profile.Directory, PAINT_STATE_FILE);
            if (System.IO.File.Exists(stateFile))
            {
                generator.LoadState(stateFile);
                if (generator.CanResume)
                {
                    beginButton.Label = "Restart";
                    stopResumeButton.Label = "Resume";
                    stopResumeButton.Sensitive = true;

                    maxRecipeSpinButton.Value = Math.Max(generator.MaxConcentration, 14); //
                    fullQuantitySpinButton.Value = generator.FullQuantity; // TODO: read/save profile info
                    fullQuantityDepthSpinButton.Value = generator.FullQuantityDepth; // TODO: read/save profile info
                    maxIngredientsSpinButton.Value = generator.MaxReagents;
                }
            }
            else
            {
                beginButton.Label = "Start";
                stopResumeButton.Label = "Stop";
                stopResumeButton.Sensitive = false;

                maxRecipeSpinButton.Value = 14;
                //fullQuantitySpinButton.Value = generator.FullQuantity; // TODO: read/save profile info
                //fullQuantityDepthSpinButton.Value = generator.FullQuantityDepth; // TODO: read/save profile info
                //maxIngredientsSpinButton.Value = generator.MaxReagents;
            }
            //generator.Log = System.IO.Path.Combine(profile.Directory, "dp_log.txt");
            countLabel.Text = String.Format("{0} / {1}", profile.RecipeCount, Palette.Count);

            colorStore.Clear();
            foreach (KeyValuePair<string, PaintRecipe> pair in profile.Recipes)
            {
                if (pair.Value.IsValidForConcentration(PaintRecipe.PAINT_RECIPE_MIN_CONCENTRATION))
                {
                    string colorName = pair.Key;
                    colorStore.AppendValues(colorName);
                }
            }
            recipeList.Show();
        }

        private void InitStateForRibbons()
        {
            maxRecipeSpinButton.Adjustment.Lower = PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION;
            fullQuantitySpinButton.Adjustment.Upper = 100;
            if (generator == null)
            {
                generator = new RecipeGenerator(profile.Reactions);
                int threads;
                DesertPaintLab.AppSettings.Get("GeneratorThreads", out threads);
                if (threads <= 0) { threads = 15; }
                generator.MaxThreads = (uint)threads;

                generator.Progress += OnProgress;
                generator.Finished += OnFinished;
                generator.NewRecipe += OnNewRecipe;
            }
            generator.InitRecipes(profile.RibbonRecipes);

            string stateFile = System.IO.Path.Combine(profile.Directory, RIBBON_STATE_FILE);
            if (System.IO.File.Exists(stateFile))
            {
                generator.LoadState(stateFile);
                if (generator.CanResume)
                {
                    beginButton.Label = "Restart";
                    stopResumeButton.Label = "Resume";
                    stopResumeButton.Sensitive = true;

                    maxRecipeSpinButton.Value = Math.Max(generator.MaxConcentration, PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION); //
                    fullQuantitySpinButton.Value = generator.FullQuantity; // TODO: read/save profile info
                    fullQuantityDepthSpinButton.Value = generator.FullQuantityDepth; // TODO: read/save profile info
                    maxIngredientsSpinButton.Value = generator.MaxReagents;
                }
            }
            else
            {
                beginButton.Label = "Start";
                stopResumeButton.Label = "Stop";
                stopResumeButton.Sensitive = false;

                maxRecipeSpinButton.Value = PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION
;
                //fullQuantitySpinButton.Value = generator.FullQuantity; // TODO: read/save profile info
                //fullQuantityDepthSpinButton.Value = generator.FullQuantityDepth; // TODO: read/save profile info
                //maxIngredientsSpinButton.Value = generator.MaxReagents;
            }

            countLabel.Text = String.Format("{0} / {1}", profile.RibbonCount, Palette.Count);

            colorStore.Clear();
            foreach (KeyValuePair<string, PaintRecipe> pair in profile.RibbonRecipes)
            {
                if (pair.Value.IsValidForConcentration(PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION))
                {
                    string colorName = pair.Key;
                    colorStore.AppendValues(colorName);
                }
            }
            recipeList.Show();
        }

        public PlayerProfile Profile {
            set {
                if (profile != value) {
                    // TODO: ensure not running
                    profile = value;
                    profile.LoadRecipes();

                    generator = new RecipeGenerator(profile.Reactions);
                    int threads;
                    DesertPaintLab.AppSettings.Get("GeneratorThreads", out threads);
                    if (threads <= 0) { threads = 15; }
                    generator.MaxThreads = (uint)threads;
        
                    generator.Progress += OnProgress;
                    generator.Finished += OnFinished;
                    generator.NewRecipe += OnNewRecipe;

                    if (checkButtonRibbon.Active)
                    {
                        InitStateForRibbons();
                    }
                    else
                    {
                        InitStateForPaint();
                    }
                }
            }
        }

        protected void OnMinIngredientsChanged(object sender, EventArgs e)
        {
            Gtk.SpinButton button = (Gtk.SpinButton) sender;
            if (button.ValueAsInt > maxIngredientsSpinButton.ValueAsInt)
            {
                maxIngredientsSpinButton.Value = button.ValueAsInt;
            }
            maxIngredientsSpinButton.SetRange(button.ValueAsInt, maxIngredientsSpinButton.Adjustment.Upper);
        }

        protected void OnMaxIngredientsChanged(object sender, EventArgs e)
        {
            Gtk.SpinButton button = (Gtk.SpinButton) sender;
            if (button.ValueAsInt < minIngredientsSpinButton.ValueAsInt)
            {
                minIngredientsSpinButton.Value = button.ValueAsInt;
            }
            minIngredientsSpinButton.SetRange(1, button.ValueAsInt);
            // TODO: save profile setting
            // TODO: no longer permit resume
        }

        protected void OnMaxRecipeChanged(object sender, EventArgs e)
        {
            // TODO: save profile setting
            // TODO: no longer permit resume
        }

        protected void OnFullQuantityDepthChanged(object sender, EventArgs e)
        {
            // TODO: save profile setting
            // TODO: no longer permit resume
        }

        protected void OnFullQuantityChanged(object sender, EventArgs e)
        {
            // TODO: save profile setting
            // TODO: no longer permit resume
        }

        protected void OnBegin(object sender, EventArgs e)
        {
            minIngredientsSpinButton.Sensitive = false;
            maxIngredientsSpinButton.Sensitive = false;
            //TODO ExportToWikiAction.Sensitive = false;
            maxRecipeSpinButton.Sensitive = false;
            beginButton.Sensitive = false; // TODO: change to "pause"?
            stopResumeButton.Sensitive = true;
            fullQuantitySpinButton.Sensitive = false;
            fullQuantityDepthSpinButton.Sensitive = false;
            reagentListView.Sensitive = false;
            checkButtonRibbon.Sensitive = false;

            countLabel.Text = String.Format("{0} / {1}", checkButtonRibbon.Active ? profile.RibbonCount : profile.RecipeCount, Palette.Count);

            // TODO: hook up event notifications
            // - progress
            // - complete
            // - new recipe / recipe update

            // Total recipe search count
            //int current = ReagentManager.Names.Count;
            //long recipePermutations = 1;
            //for (int i = 0; i < maxIngredientsSpinButton.ValueAsInt; ++i)
            //{
            //    recipePermutations *= current;
            //    --current;
            //}
            //System.Console.WriteLine("Will search {0} reagent permutations.", recipePermutations);

            lastProgressUpdate = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            lastStatusUpdate = lastProgressUpdate;

            lastProfileSave = lastProgressUpdate;
            lastCheckpoint = lastProgressUpdate;

            running = true;
            canceling = false;
            pauseForCheckpoint = false;
            stopResumeButton.Label = "Pause";

            if (Started != null)
            {
                Started(this, null);
            }

            uint minConcentration = (uint)(checkButtonRibbon.Active ? PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION : PaintRecipe.PAINT_RECIPE_MIN_CONCENTRATION);
            uint maxConcentration = (uint)maxRecipeSpinButton.ValueAsInt;
            uint minIngredients = (uint)minIngredientsSpinButton.Value;
            uint maxIngredients = (uint)maxIngredientsSpinButton.ValueAsInt;
            uint fullQuantityDepth = (uint)fullQuantityDepthSpinButton.ValueAsInt;
            uint fullQuantity = (uint)fullQuantitySpinButton.ValueAsInt;
            generator.BeginRecipeGeneration(minConcentration, 
                                            maxConcentration, 
                                            minIngredients, 
                                            maxIngredients, 
                                            fullQuantityDepth, 
                                            fullQuantity);
        }

        protected void OnStopResume(object sender, EventArgs e)
        {
            if (generator != null)
            {
                if (running)
                {
                    canceling = true;
                    pauseForCheckpoint = false;
                    generator.Stop();
                }
                else
                {
                    // Resume previous run
                    //TODO ExportToWikiAction.Sensitive = false;
                    reagentListView.Sensitive = false;
                    minIngredientsSpinButton.Sensitive = false;
                    maxIngredientsSpinButton.Sensitive = false;
                    maxRecipeSpinButton.Sensitive = false;
                    beginButton.Sensitive = false;
                    stopResumeButton.Sensitive = true;
                    fullQuantitySpinButton.Sensitive = false;
                    fullQuantityDepthSpinButton.Sensitive = false;
                    reagentListView.Sensitive = false;
                    checkButtonRibbon.Sensitive = false;

                    lastProgressUpdate = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                    lastStatusUpdate = lastProgressUpdate;
                    lastProfileSave = lastProgressUpdate;
                    lastCheckpoint = lastProgressUpdate;

                    canceling = false;
                    pauseForCheckpoint = false;
                    running = true;
        
                    stopResumeButton.Label = "Pause";

                    if (Started != null)
                    {
                        Started(this, null);
                    }
                    generator.ResumeRecipeGeneration();
                }
            }
        }

        private void HandleFinished()
        {
            generator.Wait();
            if (pauseForCheckpoint)
            {
                pauseForCheckpoint = false;
                generator.SaveState(System.IO.Path.Combine(profile.Directory, checkButtonRibbon.Active ? RIBBON_STATE_FILE : PAINT_STATE_FILE));
                generator.ResumeRecipeGeneration();
            }
            else
            {
                running = false;
                beginButton.Sensitive = true;
                //TODO ExportToWikiAction.Sensitive = true;
                stopResumeButton.Sensitive = false;
                minIngredientsSpinButton.Sensitive = true;
                maxIngredientsSpinButton.Sensitive = true;
                maxRecipeSpinButton.Sensitive = true;
                fullQuantitySpinButton.Sensitive = true;
                fullQuantityDepthSpinButton.Sensitive = true;
                reagentListView.Sensitive = true;
                checkButtonRibbon.Sensitive = true;

                //generator = null; // don't. Hang on to generator for resume.
                profile.SaveRecipes();
                if (canceling)
                {
                    generator.SaveState(System.IO.Path.Combine(profile.Directory, checkButtonRibbon.Active ? RIBBON_STATE_FILE : PAINT_STATE_FILE));
                    stopResumeButton.Label = "Resume";
                    stopResumeButton.Sensitive = true;
                    beginButton.Label = "Restart";
                }
                else
                {
                    System.IO.File.Delete(System.IO.Path.Combine(profile.Directory, checkButtonRibbon.Active ? RIBBON_STATE_FILE : PAINT_STATE_FILE));
                }
                if (Stopped != null)
                {
                    Stopped(this, null);
                }
            }
        }

        protected void OnFinished(object sender, EventArgs args)
        {
            notifyFinished.WakeupMain();
            //Gtk.Application.Invoke(delegate {
            //    HandleFinished();
            //});
        }

        private void HandleNewRecipe()
        {
            progressBar.Pulse();
            
            PaintRecipe recipe = null;
            if (pendingNewRecipes.TryDequeue(out recipe))
            {
                string recipeColor = Palette.FindNearest(recipe.ReactedColor);
                // TODO: Add item to recipe list only if not already listed
                bool exists = false;
                Gtk.TreeIter iter;
                if (colorStore.GetIterFirst(out iter))
                {
                    do
                    {
                        string color = (string)colorStore.GetValue(iter, 0);
                        if (color.Equals(recipeColor))
                        {
                            exists = true;
                            break;
                        }
                    } while (colorStore.IterNext(ref iter));
                }
                if (!exists)
                {
                    //Console.WriteLine("Add new recipe for {0}", recipeColor);
                    //    bool isMissingReactions = args.Recipe.CheckMissingReactions(ref missingReactions);
                    //    string missingReactionLabel = isMissingReactions ? "X" : "";
                    colorStore.AppendValues(recipeColor); // , missingReactionLabel);
                    countLabel.Text = String.Format("{0} / {1}", (checkButtonRibbon.Active ? profile.RibbonCount : profile.RecipeCount)+1, Palette.Count);
                }
                //else
                //{
                //    Console.WriteLine("Updated recipe for {0}", recipeColor);
                //}
                if (checkButtonRibbon.Active)
                {
                    profile.SetRibbonRecipe(recipe);
                }
                else
                {
                    profile.SetRecipe(recipe);
                }
    
                long progressTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                long delta = progressTime - lastProfileSave;
                if (delta >= RECIPE_SAVE_INTERVAL)
                {
                    profile.SaveRecipes();
                    lastProfileSave = progressTime;
                }
                Gtk.TreeModel model;
                Gtk.TreeSelection selection = recipeList.Selection;
                if ((selection != null) && selection.GetSelected(out model, out iter))
                {
                    string colorName = (string)colorStore.GetValue(iter, 0);
                    if (colorName.Equals(recipeColor))
                    {
                        ShowColor(recipe);
                    }
                }
            }
        }

        protected void OnNewRecipe(object sender, NewRecipeEventArgs args)
        {
            PaintRecipe recipe = new PaintRecipe(args.Recipe); // copy it, so the worker thread can release
            lock(this) {
                pendingNewRecipes.Enqueue(recipe);
            }
            notifyNewRecipe.WakeupMain();
            //Gtk.Application.Invoke(delegate {
            //    HandleNewRecipe();
            //});
        }

        private void HandleProgress()
        {
            long progressTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            long delta = progressTime - lastProgressUpdate;
            if (delta > 30)
            {
                lastProgressUpdate = progressTime;
                progressBar.Pulse();
            }
            delta = progressTime - lastStatusUpdate;
            if (delta > 500)
            {
                lastStatusUpdate = progressTime;
                if (SetStatus != null)
                {
                    SetStatus(this, new StatusUpdateEventArgs(String.Format("Recipes searched: {0:N00}", generator.RecipeCount)));
                }
                //TODO statusLabel.Text = String.Format("Recipes searched: {0:N00}", generator.RecipeCount);
            }
            delta = progressTime - lastCheckpoint;
            if (delta > CHECKPOINT_INTERVAL)
            {
                lastCheckpoint = progressTime;
                pauseForCheckpoint = true;
                generator.Stop();
            }
        }

        protected void OnProgress(object sender, EventArgs args)
        {
            notifyProgress.WakeupMain();
        }

        private void ShowColor(PaintRecipe recipe)
        {
            Gtk.ListStore store = (Gtk.ListStore)recipeIngredientsView.Model;
            store.Clear();
            if (recipe.CheckMissingReactions(ref missingReactions))
            {
                if (SetStatus != null)
                {
                    SetStatus(this, new StatusUpdateEventArgs("WARNING: This recipe includes reactions that have not yet been recorded."));
                }
                //TODO statusLabel.Text = "WARNING: This recipe includes reactions that have not yet been recorded.";
            }
            foreach (PaintRecipe.RecipeIngredient ingredient in recipe.Ingredients)
            {
                store.AppendValues(ingredient.quantity.ToString(), ingredient.name);
            }
            paintSwatch.Color = recipe.ReactedColor;
        }

        protected void OnColorSelected(object o, EventArgs args)
        {
            Gtk.TreeModel model;
            Gtk.TreeIter iter;
            Gtk.TreeSelection selection = recipeList.Selection;
            if ((selection != null) && selection.GetSelected(out model, out iter))
            {
                string colorName = (string)colorStore.GetValue(iter, 0);
                PaintRecipe recipe;
                if (checkButtonRibbon.Active)
                {
                    if (profile.RibbonRecipes.TryGetValue(colorName, out recipe))
                    {
                        ShowColor(recipe);
                    }
                }
                else
                {
                    if (profile.Recipes.TryGetValue(colorName, out recipe))
                    {
                        ShowColor(recipe);
                    }
                }
            }
        }

        protected void OnShowIngredients(object sender, EventArgs e)
        {
            ReagentWindow win = new ReagentWindow(profile);
            win.Show();
        }

        protected void OnDestroyed(object o, EventArgs args)
        {
            if (running)
            {
                // window closed while generator running: stop and save
                generator.Finished -= OnFinished;
                generator.Progress -= OnProgress;
                generator.NewRecipe -= OnNewRecipe;
                generator.Stop();
                generator.Wait();
                generator.SaveState(System.IO.Path.Combine(profile.Directory, checkButtonRibbon.Active ? RIBBON_STATE_FILE : PAINT_STATE_FILE));
            }
        }

        // Reagent view handling
        private void RenderReagentToggle (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
        {
            Reagent reagent = (Reagent) model.GetValue (iter, 0);
            Gtk.CellRendererToggle toggle = (cell as Gtk.CellRendererToggle);
            toggle.Active = reagent.Enabled;
            toggle.Activatable = !reagent.IsCatalyst;
            toggle.Mode = reagent.IsCatalyst ? Gtk.CellRendererMode.Inert : Gtk.CellRendererMode.Activatable;
        }
        private void RenderReagentCost (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
        {
            Reagent reagent = (Reagent) model.GetValue (iter, 0);
            (cell as Gtk.CellRendererText).Text = reagent.Cost.ToString();
        }
        private void RenderReagentQuantity (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
        {
            Reagent reagent = (Reagent) model.GetValue (iter, 0);
            (cell as Gtk.CellRendererText).Text = reagent.RecipeMax.ToString();
        }

        private void OnReagentCostChanged(object o, Gtk.EditedArgs args)
        {
            uint newCost;
            if (uint.TryParse(args.NewText, out newCost))
            {
                Gtk.TreeIter iter;
                reagentListStore.GetIter (out iter, new Gtk.TreePath (args.Path));
             
                Reagent reagent = (Reagent) reagentListStore.GetValue (iter, 0);
                if (reagent.Cost != newCost)
                {
                    reagent.Cost = newCost;
                    SaveReagentSettings();
                }
            }
        }

        private void OnReagentQuantityChanged(object o, Gtk.EditedArgs args)
        {
            uint newMax;
            if (uint.TryParse(args.NewText, out newMax))
            {
                Gtk.TreeIter iter;
                reagentListStore.GetIter (out iter, new Gtk.TreePath (args.Path));
             
                Reagent reagent = (Reagent) reagentListStore.GetValue (iter, 0);
                if (reagent.RecipeMax != newMax)
                {
                    reagent.RecipeMax = newMax;
                    SaveReagentSettings();
                }
            }
        }

        private void OnReagentEnableToggled(object o, Gtk.ToggledArgs args)
        {
            Gtk.TreeIter iter;
            reagentListStore.GetIter (out iter, new Gtk.TreePath (args.Path));
         
            Reagent reagent = (Reagent) reagentListStore.GetValue (iter, 0);
            reagent.Enabled = !reagent.Enabled;
            SaveReagentSettings();
        }

        private void SaveReagentSettings()
        {
            // save out state
            ReagentManager.SaveProfileReagents(profile.ReagentFile);
        }

        protected void OnCopyRecipeToClipboard(object sender, EventArgs e)
        {
            Gtk.TreeModel model;
            Gtk.TreeIter iter;
            Gtk.Clipboard clipboard = recipeIngredientsView.GetClipboard(Gdk.Selection.Clipboard);
            
            Gtk.TreeSelection selection = recipeList.Selection;
            if ((selection != null) && selection.GetSelected(out model, out iter))
            {
                string colorName = (string)colorStore.GetValue(iter, 0);
                PaintRecipe recipe;
                if (checkButtonRibbon.Active)
                {
                    if (profile.RibbonRecipes.TryGetValue(colorName, out recipe))
                    {
                        clipboard.Text = recipe.ToString();
                    }
                }
                else
                {
                    if (profile.Recipes.TryGetValue(colorName, out recipe))
                    {
                        clipboard.Text = recipe.ToString();
                    }
                }
            }
        }

        protected void OnRecipesToggled(object sender, EventArgs e)
        {
            if (checkButtonRibbon.Active)
            {
                // changed to ribbons
                InitStateForRibbons();
            }
            else
            {
                // changed to paints
                InitStateForPaint();
            }
            profile.ProfileSettings.Set("Generator.Ribbons", checkButtonRibbon.Active);
            profile.Save();
        }
    }
}