// Autogenerated with DRAKON Editor 1.31
using System;
using System.Collections.Generic;

namespace Drakon.Editor.Example {

public class Demo {


    public static List<int> Fibonacci(int n) {
        // item 185
        var result = new List<int> { 0 };
        // item 1610001
        if (n == 0) {
            
        } else {
            // item 1610002
            if (n == 1) {
                // item 187
                result.Add(1);
            } else {
                // item 188
                result.Add(1);
                // item 1710001
                int i = 2;
                while (true) {
                    // item 1710002
                    if (i <= n) {
                        
                    } else {
                        break;
                    }
                    // item 172
                    int f2 = result[i - 2];
                    int f1 = result[i - 1];
                    int fib = f1 + f2;
                    // item 173
                    result.Add(fib);
                    // item 1710003
                    i++;
                }
            }
        }
        // item 176
        return result;
    }

    public static void Main() {
        // item 52
        Console.WriteLine("DRAKON-C# demo");
        Console.WriteLine("==============");
        // item 189
        ForeachDemo();
        alt_select_test();
        // item 50
        QuicksortDemo();
        // item 190
        int result = 0;
        // item 191
        Console.WriteLine(result);
    }

    private static void ForeachDemo() {
        // item 155
        Console.WriteLine("iteration demo");
        // item 153
        List<int> sequence = Fibonacci(15);
        // item 154
        PrintListArrow(sequence);
        PrintListFor(sequence);
        PrintListForeach(sequence);
        // item 156
        Console.WriteLine();
        // item 291
        TestEarly(20, 60);
        // item 292
        TestEarly(35, 70);
        // item 293
        TestEarly(4, 0);
    }

    private static int ForeachEarlyExit(int value) {
        // item 279
        int found = -1;
        int normal_262;
        normal_262 = 1;
        foreach (int x in new int[] {10, 20, 30}) {
            // item 266
            if (x == value) {
                // item 270
                found = x;
                normal_262 = 0;
                break;
            } else {
                
            }
        }
        if (normal_262 == 1) {
            // item 269
            found = 0;
        }
        foreach (int x in new int[] {40, 20, 70}) {
            // item 278
            int m = x / 2;
            // item 273
            if (m == value) {
                // item 277
                return x + found;
            } else {
                
            }
        }
        // item 276
        return 0;
    }

    private static void Print(Object[] collection) {
        foreach (Object item in collection) {
            // item 98
            Write(item);
        }
        // item 99
        Console.WriteLine();
    }

    private static void PrintListArrow(List<int> collection) {
        // item 131
        Console.WriteLine("using if and arrow:");
        // item 126
        int length = collection.Count;
        int i = 0;
        while (true) {
            // item 127
            if (i < length) {
                
            } else {
                break;
            }
            // item 125
            int item = collection[i];
            Write(item);
            // item 129
            i++;
        }
        // item 132
        Console.WriteLine("");
    }

    private static void PrintListFor(List<int> collection) {
        // item 142
        Console.WriteLine("using for:");
        // item 184
        int length = collection.Count;
        // item 1390001
        int i = 0;
        while (true) {
            // item 1390002
            if (i < length) {
                
            } else {
                break;
            }
            // item 183
            Write(collection[i]);
            // item 1390003
            i += 1;
        }
        // item 143
        Console.WriteLine("");
    }

    private static void PrintListForeach(IEnumerable<int> collection) {
        // item 119
        Console.WriteLine("using foreach:");
        foreach (int item in collection) {
            // item 116
            Write(item);
        }
        // item 118
        Console.WriteLine("");
    }

    private static void QuicksortDemo() {
        // item 62
        Console.WriteLine("quick sort demo");
        // item 58
        var unsorted = new Object[] { "the", "sooner", "we", "start", "this", "the", "better" };
        var sorted   = new Object[] { "aa", "bb", "cc", "dd", "ee", "ff" };
        var reverse  = new Object[] { "ff", "ee", "dd", "cc", "bb", "aa" };
        var empty    = new Object[] {};
        var flat     = new Object[] { "flat", "flat", "flat", "flat", "flat" };
        // item 59
        IComparer<Object> comparer = new ReverseStringComparer();
        // item 198
        Sort(unsorted, comparer);
        Sort(sorted, comparer);
        Sort(reverse, comparer);
        Sort(empty, comparer);
        Sort(flat, comparer);
        // item 60
        Print(unsorted);
        Print(sorted);
        Print(reverse);
        Print(empty);
        Print(flat);
        // item 61
        StringsAreSorted(unsorted);
        StringsAreSorted(sorted);
        StringsAreSorted(reverse);
        StringsAreSorted(empty);
        StringsAreSorted(flat);
        // item 65
        Console.WriteLine();
    }

    private static void Sort(Object[] list, IComparer<Object> comparer) {
        // item 197
        Sorter.Quicksort(comparer, list, 0, list.Length);
    }

    private static void StringsAreSorted(Object[] array) {
        IEnumerator<object> _it71 = null;
        object obj = default(object);
        int _sw810000_ = 0;
        int i = 0, j = 0;
        int length = array.Length;
        string after = null;
        string current = null;
        int _next_item_ = 0;
        _next_item_ = 710001;
        while (true) {
            if (_next_item_ == 710001) {
                _it71 = ((IEnumerable<object>)array).GetEnumerator();
                _next_item_ = 710002;
        
            } else if (_next_item_ == 710002) {
                if (_it71.MoveNext()) {
                    // item 710004
                    obj = _it71.Current;
                    _next_item_ = 249;
                } else {
                    return;
                }
        
            } else if (_next_item_ == 249) {
                current = (string)obj;
                _next_item_ = 740001;
        
            } else if (_next_item_ == 740001) {
                j = i + 1;
                _next_item_ = 740002;
        
            } else if (_next_item_ == 740002) {
                if (j < length) {
                    // item 76
                    after = (String)array[j];
                    _next_item_ = 810000;
                } else {
                    // item 248
                    i++;
                    _next_item_ = 710002;
                }
        
            } else if (_next_item_ == 810000) {
                _sw810000_ = current.CompareTo(after);
                _next_item_ = 810001;
        
            } else if (_next_item_ == 810001) {
                if (_sw810000_ == 1) {
                    // item 90
                    _next_item_ = 740003;
                } else {
                    _next_item_ = 810002;
                }
        
            } else if (_next_item_ == 810002) {
                if (_sw810000_ == 0) {
                    // item 89
                    _next_item_ = 740003;
                } else {
                    _next_item_ = 810003;
                }
        
            } else if (_next_item_ == 740003) {
                j += 1;
                _next_item_ = 740002;
        
            } else if (_next_item_ == 810003) {
                if (_sw810000_ == -1) {
                    _next_item_ = 88;
                } else {
                    // item 810004
                    throw new InvalidOperationException("Not expected:  " + _sw810000_.ToString());
                }
        
            } else if (_next_item_ == 88) {
                _next_item_ = 77;
        
            } else if (_next_item_ == 77) {
                throw new InvalidOperationException("Collection is not sorted.");
        
            }
        }
    }

    private static void TestEarly(int value, int expected) {
        // item 285
        int actual = ForeachEarlyExit(value);
        // item 286
        if (actual == expected) {
            
        } else {
            // item 289
            throw new Exception("foreach_early_exit failed: " + actual + " " + expected);
        }
    }

    private static void Write(Object item) {
        // item 180
        Console.Write(item);
        Console.Write(" ");
    }

    static void alt_select_test() {
        // item 202
        alternative_select(-100, -1);
        alternative_select(100, 1);
        alternative_select(0, 0);
    }

    static void alternative_select(int value, int expected) {
        // item 208
        int actual;
        // item 2090001
        if (value < 0) {
            // item 217
            actual = -1;
        } else {
            // item 2090002
            if (value == 0) {
                // item 218
                actual = 0;
            } else {
                // item 219
                actual = 1;
            }
        }
        // item 220
        if (actual == expected) {
            
        } else {
            // item 226
            throw new 
            Exception("alt select failure!\n");
        }
        // item 2300001
        if (value < 0) {
            // item 238
            actual = -1;
        } else {
            // item 2300002
            if (value == 0) {
                // item 239
                actual = 0;
            } else {
                // item 2300003
                if (value > 0) {
                    
                } else {
                    // item 2300004
                    throw new InvalidOperationException("Condition was not detected.");
                }
                // item 240
                actual = 1;
            }
        }
        // item 241
        if (actual == expected) {
            
        } else {
            // item 245
            throw new 
            Exception("alt select failure!\n");
        }
    }
}
// Символы Юникод
}
