/*/Function initWindowManger()
        //Purpose: This function is just to intialize all 80 windows
        //		   with filler info to be put into the WindowManger array of all windows.
        //		   this is for testing. Syncing with DB with HTTP request is how this will
        //		   usually be done and that is done on the individual platforms.
        public void initWindowManger()
        {
            secLetter = 'a';
            int id = 1;
            current_COL = 'a';
            current_ROW = 1;

            for (int i = 0; i < section_count; i++)
            {
                if (i == 0 || i == 25) {
                    for (int j = 0; j < 4; j++) {
                        Window win = new Window ();
                        win.WindowID = id;
                        //win.WindowCol = secLetter;
                        win.WindowNum = j + 1;
                        win.WindowTitle = string.Format ("WINDOW {0}{1} ID:{2}",win.WindowCol,win.WindowNum,win.WindowID);
                        win.WindowImagePath = string.Format ("win_{0}{1}.JPG",win.WindowCol,win.WindowNum);
                        win.WindowDiscription = string.Format ("This is a nice picture of image: {0}{1}", win.WindowCol, win.WindowNum);
                        string identifier = string.Format ("{0}{1}",win.WindowCol,win.WindowNum);
                        windowsDictionary.Add (identifier, win);
                        id++;
                    }
                } else {
                    for (int j = 0; j < 3; j++) {
                        Window win = new Window ();
                        win.WindowID = id;
                        //win.WindowCol = secLetter;
                        win.WindowNum = j + 1;
                        win.WindowTitle = string.Format ("WINDOW {0}{1} ID:{2}",win.WindowCol,win.WindowNum,win.WindowID);
                        win.WindowImagePath = string.Format ("win_{0}{1}.JPG",win.WindowCol,win.WindowNum);
                        win.WindowDiscription = string.Format ("This is a nice picture of image: {0}{1}", win.WindowCol, win.WindowNum);
                        string identifier = string.Format ("{0}{1}",win.WindowCol,win.WindowNum);
                        windowsDictionary.Add (identifier, win);
                        id++;
                    }
                }
                secLetter++;

            }

        }*/
        /// <summary>
        /// 
        /// </summary>
        public void initFromDB()
        {
            //Make sure not accessing DB on different threads.

            object locker = new object ();

            lock (locker){
                var database = DependencyService.Get<ISQLite>().GetConnection ();

                //Fill Pannel Dictionary
                var pan = database.Query<Pannel> ("SELECT * FROM Pannel");
                foreach (var p in pan) {
                    pannels_Dictionary.Add (p.Id, p);
                }

                //Fill Section Dictionary
                var sec = database.Query<Section_Mobile>("SELECT * FROM Section_Mobile");
                foreach (var s in sec) {
                    sections_Dictionary.Add (s.Id, s);
                }

                //Fill ImageMapping Dictionary
                var im = database.Query<ImageMapping>("SELECT * FROM ImageMapping");
                foreach (var i in im){
                    imageMapping_Dictionary.Add (i.Id, i);
                }

                //Fill in Pannel to Image Mapping Dictionary
                //get number of distint pannels with image mappings in them
                var quary = database.Query<ImageMapping>("SELECT DISTINCT PannelID FROM ImageMapping");
                int count = 0;
                foreach (var c in quary) {
                    count++;
                }
                int[] pannels = new int[count];
                int index = 0;
                foreach (var c in quary) {
                    pannels[index] = c.PannelID;
                    index++;
                }
                string q;
                foreach (int val in pannels) {
                    q = string.Format ("SELECT * FROM ImageMapping WHERE PannelID={0}",val);
                    quary = database.Query<ImageMapping> (q);
                    int num = 0;
                    foreach(var v in quary){
                        num++;
                    }
                    ImageMapping[] imageMaps = new ImageMapping[num];
                    int j = 0;
                    foreach (var t in quary) {
                        imageMaps [j] = t;
                        j++;
                    }
                    pannelToImageMapping_Dictionary.Add (val, imageMaps);
                }

                //Fille in Setion to Pannel Dictionary
                Pannel [] pToS;
                for (int i = 1; i <= 26; i++) {
                    string e = string.Format ("SELECT * FROM Pannel WHERE Column={0}",i);
                    var qu = database.Query<Pannel> (e);
                    if (i == 1 || i == 26) {
                        pToS = new Pannel[4];
                        int j = 0;
                        foreach(var qq in qu){
                            pToS[j] = qq;
                            j++;
                        }
                        sectionToPannels_Dictionary.Add (i, pToS);
                    } else {
                        pToS = new Pannel[3];
                        int j = 0;
                        foreach (var ww in qu) {
                            pToS [j] = ww;
                            j++;
                        }
                        sectionToPannels_Dictionary.Add (i, pToS);
                    }

                }

            }//end of lock
        }
        void loadInfoInView()
        {
            if (handler is Section_Mobile) {
                section = (Section_Mobile)handler;
                section_MobileHandler ();

            } else if (handler is Pannel) {
                pannel = (Pannel)handler;
                pannel_Handler ();

            } else if (handler is ImageMapping) {
                imageMapping = (ImageMapping)handler;
                imageMapping_Handler ();
            }
        }