from collections import OrderedDict
import math
from copy import deepcopy
from numpy.random import randint
import inspect
import itertools
import json
import numpy as np
import os
import sys
from numpy.random import uniform


class Dump:# {{{
    def __init__(self,*args):
        '''debugging function, much like print but handles various types better'''
        print()
        for struct in args:
            if isinstance(struct, list):
                for i in struct:
                    print(i)
            elif isinstance(struct, tuple):
                for i in struct:
                    print(i)
            elif isinstance(struct, dict):
                for k, v in struct.items():
                    print (str(k)+':', v)
            else:
                print(struct)
# }}}
class Json: # {{{
    def readdb(self,table):
        if not hasattr(self, 's'):
            self.s=Sqlite("{}/aamks.sqlite".format(os.environ['AAMKS_PROJECT']))
        return json.loads(self.s.query("SELECT json FROM {}".format(table))[0]['json'])
    def read(self,path): 
        try:
            f=open(path, 'r')
            dump=json.load(f, object_pairs_hook=OrderedDict)
            f.close()
            return dump
        except:
            raise SystemExit("include.py: Missing or invalid json: {}.".format(path)) 

    def write(self, data, path, pretty=0): 
        try:
            if pretty==1:
                pretty=json.dumps(data, indent=4)
                with open(path, "w") as f: 
                    json.dump(pretty, f)
            else:
                with open(path, "w") as f: 
                    json.dump(data, f)
        except:
            raise SystemExit("include.py: Cannot write json: {}.".format(path)) 


# }}}

class Gen:
    def __init__(self):# {{{
        self.rings={}
        self.num_frames=100
        for r in range(1,6):
            self.rings[r]=self.make_polygon(r)
        self.ring_frames()
        #self.plot_rings()
# }}}
    def plot_rings(self):# {{{
        import matplotlib.pyplot as plt
        dots=[ [], [] ]
        for r,v in self.rings.items():
            for i in v:
                dots[0].append(i[0])
                dots[1].append(i[1])
        plt.scatter(dots[0], dots[1])
        plt.show()
        
# }}}
    def make_polygon(self,radius):# {{{
        sides=22 + radius*10
        one_segment = math.pi * 2 / sides
        points = [ (math.sin(one_segment * i) * (1+0.1 * radius), math.cos(one_segment * i) * (1+0.1 * radius), uniform(0.4,0.6)) for i in range(sides)]
        return points
# }}}
    def ring_frames(self):# {{{
        self.frames=[]
        for frame in range(1,self.num_frames):
            frame_record={}
            for r in self.rings.keys(): 
                try:
                    new=deepcopy(self.frames[frame-2][r])
                except:
                    new=deepcopy(self.rings[r])
                first=new.pop(0)
                new.append(first)
                frame_record[r]=new
            self.frames.append(frame_record)

        flat=[]
        for f in self.frames:
            record=[]
            for m,n in f.items():
                record+=n
            flat.append(record)

        #flat=[ [(0,0) ], [(2,0) ], [(4,0) ], [(6,0) ] ]
        
        j.write(flat, "ciasto.json")
    # }}}
        
# }}}

dd=Dump
j=Json()
Gen()
# ciasto.json
