from flask_restful import Resource, reqparse
import pymongo
from bson.json_util import dumps
from bson.objectid import ObjectId
import json
import time
from flask import jsonify



'''
API endpoints for the FM information
'''



class fm_live(Resource):
    '''
    GET live FM data
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def get(self):
        # Get live data
        fm_live = self.DB['fm_live'].find().sort("name", pymongo.ASCENDING)
        return(jsonify(json.loads(dumps(fm_live))))



class fm_modules(Resource):
    '''
    GET FM modules
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def get(self):
        # Get fm modules
        fm_modules = self.DB['fm_modules'].find().sort("name", pymongo.ASCENDING)
        fm_live = self.DB['fm_live'].find()

        lives = {}
        for live in fm_live:
            lives[live['module_uid']] = live

        modules = []
        for module in fm_modules:
            data = {
                'location': module['location'],
                'type': module['type'],
                'ip': module['ip'],
                'station': module['station'],
                'url': lives[str(module['_id'])]['url'],
                'uid': str(module['_id'])
            }
            modules.append(data)

        return(jsonify(json.loads(dumps(modules))))



class fm_create(Resource):
    '''
    POST to create an fm module
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def post(self):
        # Initilize request parser
        parser = reqparse.RequestParser()
        # Parse arguments from form data
        parser.add_argument("station")
        parser.add_argument("type")
        parser.add_argument("location")
        parser.add_argument("ip")
        parser.add_argument("url")
        args = parser.parse_args()

        module_data = {
            "station": args['station'],
            "type": args['type'],
            "location": args['location'],
            "ip": args['ip'],
        }

        result = self.DB['fm_modules'].insert_one(module_data)
        _id = str(result.inserted_id)
        self.DB['fm_live'].insert_one(
            {
                "station": args['station'],
                "url" : args['url'],
                "module_uid": _id,
            }
        )
        return({'success': True, 'message': f"Created {args['station']}"})



class fm_update(Resource):
    '''
    POST to update an fm module
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def post(self):
        # Initilize request parser
        parser = reqparse.RequestParser()

        # Parse arguments from form data
        parser.add_argument("station")
        parser.add_argument("type")
        parser.add_argument("location")
        parser.add_argument("ip")
        parser.add_argument("url")
        parser.add_argument("uid")
        args = parser.parse_args()

        try:
            self.DB['fm_modules'].find_one_and_update(
                {
                    "_id": ObjectId(args['uid']),
                },
                {"$set":
                    {
                        "station": args['station'],
                        "type": args['type'],
                        "location": args['location'],
                        "ip": args['ip'],
                    }
                 }
            )
            
            # Update FM live DB
            self.DB['fm_live'].find_one_and_update(
                {
                    "module_uid": args['uid'],
                },
                {"$set":
                    {
                        "station": args['station'],
                        "url": args['url'],
                    }
                 }
            )

            # If it is an odroid, update it's URL and Station name
            if args['type'] == 'Odroid':
                try:
                    odroid_client = pymongo.MongoClient(f"mongodb://{args['ip']}:{self.env.mongodb_port}")
                    odroid_db = odroid_client['fm_stream']
                    odroid_db['fm_live'].insert_one(
                        {
                            "station": args['station'],
                            "url": args['url'],
                            "changed": time.time()
                        }

                    )
                except:
                    return({'success': False, 'message': f"Unable to update {args['type']} {args['station']}"})

            return({'success': True, 'message': 'Updated {}'.format(args['station'])})
        except Exception as e:
            return({'success': False, 'message': f'{str(e)}'})


class fm_delete(Resource):
    '''
    DELETE an fm module
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def delete(self, oid):
        if oid:
            module_result = self.DB['fm_modules'].delete_many(
                {
                    "_id": ObjectId(oid),
                },
            )
            live_result = self.DB['fm_live'].delete_many(
                {
                    "module_uid": oid,
                },
            )
            total_del = module_result.deleted_count + live_result.deleted_count

            if total_del > 0:
                return({'success': True, 'message': 'Deleted {} objects.'.format(total_del)})
            else:
                return({'success': False, 'message': '{} not found.'.format(oid)})
        else:
            return({'success': False})
from flask_restful import Resource, reqparse
import pymongo
from bson.json_util import dumps
from bson.objectid import ObjectId
import json
import time
from flask import jsonify



'''
API endpoints for the FM information
'''



class fm_live(Resource):
    '''
    GET live FM data
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def get(self):
        # Get live data
        fm_live = self.DB['fm_live'].find().sort("name", pymongo.ASCENDING)
        return(jsonify(json.loads(dumps(fm_live))))



class fm_modules(Resource):
    '''
    GET FM modules
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def get(self):
        # Get fm modules
        fm_modules = self.DB['fm_modules'].find().sort("name", pymongo.ASCENDING)
        fm_live = self.DB['fm_live'].find()

        lives = {}
        for live in fm_live:
            lives[live['module_uid']] = live

        modules = []
        for module in fm_modules:
            data = {
                'location': module['location'],
                'type': module['type'],
                'ip': module['ip'],
                'station': module['station'],
                'url': lives[str(module['_id'])]['url'],
                'uid': str(module['_id'])
            }
            modules.append(data)

        return(jsonify(json.loads(dumps(modules))))



class fm_create(Resource):
    '''
    POST to create an fm module
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def post(self):
        # Initilize request parser
        parser = reqparse.RequestParser()
        # Parse arguments from form data
        parser.add_argument("station")
        parser.add_argument("type")
        parser.add_argument("location")
        parser.add_argument("ip")
        parser.add_argument("url")
        args = parser.parse_args()

        module_data = {
            "station": args['station'],
            "type": args['type'],
            "location": args['location'],
            "ip": args['ip'],
        }

        result = self.DB['fm_modules'].insert_one(module_data)
        _id = str(result.inserted_id)
        self.DB['fm_live'].insert_one(
            {
                "station": args['station'],
                "url" : args['url'],
                "module_uid": _id,
            }
        )
        return({'success': True, 'message': f"Created {args['station']}"})



class fm_update(Resource):
    '''
    POST to update an fm module
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def post(self):
        # Initilize request parser
        parser = reqparse.RequestParser()

        # Parse arguments from form data
        parser.add_argument("station")
        parser.add_argument("type")
        parser.add_argument("location")
        parser.add_argument("ip")
        parser.add_argument("url")
        parser.add_argument("uid")
        args = parser.parse_args()

        try:
            self.DB['fm_modules'].find_one_and_update(
                {
                    "_id": ObjectId(args['uid']),
                },
                {"$set":
                    {
                        "station": args['station'],
                        "type": args['type'],
                        "location": args['location'],
                        "ip": args['ip'],
                    }
                 }
            )
            
            # Update FM live DB
            self.DB['fm_live'].find_one_and_update(
                {
                    "module_uid": args['uid'],
                },
                {"$set":
                    {
                        "station": args['station'],
                        "url": args['url'],
                    }
                 }
            )

            # If it is an odroid, update it's URL and Station name
            if args['type'] == 'Odroid':
                try:
                    odroid_client = pymongo.MongoClient(f"mongodb://{args['ip']}:{self.env.mongodb_port}")
                    odroid_db = odroid_client['fm_stream']
                    odroid_db['fm_live'].insert_one(
                        {
                            "station": args['station'],
                            "url": args['url'],
                            "changed": time.time()
                        }

                    )
                except:
                    return({'success': False, 'message': f"Unable to update {args['type']} {args['station']}"})

            return({'success': True, 'message': 'Updated {}'.format(args['station'])})
        except Exception as e:
            return({'success': False, 'message': f'{str(e)}'})


class fm_delete(Resource):
    '''
    DELETE an fm module
    '''
    def __init__(self, **kwargs):
        self.DB = kwargs['DB']
        self.env = kwargs['env']


    def delete(self, oid):
        if oid:
            module_result = self.DB['fm_modules'].delete_many(
                {
                    "_id": ObjectId(oid),
                },
            )
            live_result = self.DB['fm_live'].delete_many(
                {
                    "module_uid": oid,
                },
            )
            total_del = module_result.deleted_count + live_result.deleted_count

            if total_del > 0:
                return({'success': True, 'message': 'Deleted {} objects.'.format(total_del)})
            else:
                return({'success': False, 'message': '{} not found.'.format(oid)})
        else:
            return({'success': False})
API endpoints for the FM information GET live FM data # Get live data GET FM modules # Get fm modules POST to create an fm module # Initilize request parser # Parse arguments from form data POST to update an fm module # Initilize request parser # Parse arguments from form data # Update FM live DB # If it is an odroid, update it's URL and Station name DELETE an fm module