from rest_framework import serializers


class FieldMixin(object):
    """
       Turning to mixin / This class maybe extended to futureproofing stuffs/
       Can override the get_fields_names to get a specific field on our serializer

    """
    # TODO : TO be customized fully---> then make it more usable

    def get_fields_names(self, *args, **kwargs):
        """
        :param args:
        :param kwargs:
        :return:
        """
        fields_names = self.context.get('fields', None)
        if fields_names:
            return fields_names
        return super(FieldMixin, self).get_fields_names(*args, **kwargs)


class APISerializer(serializers.BaseSerializer):

    def to_representation(self, data):
        success = True
        payload = data
        if isinstance(data, dict):
            if 'success' in data:
                success = data.pop('success')
            if 'payload' in data:
                payload = data.pop('payload')
            if 'reason' in data:
                reason = data.get('reason')
                self.context['reason'] = reason
        self.context['success'] = success

        return dict(self.context, **{
            'payload': payload})
