from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
from rest_framework import serializers
from parts import models as parts_models

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = get_user_model()
        fields = ['id', 'username', 'email', 'first_name', 'last_name', 'groups']

class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ['url', 'id', 'url', 'name']

class PackageSerializerNoLink(serializers.ModelSerializer):
    class Meta:
        model = parts_models.Package
        fields = '__all__'

class PackageSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()
    class Meta:
        model = parts_models.Package
        fields = '__all__'

class ComponentParameterSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()
    ro_parameter_type = serializers.ReadOnlyField(source='parameter_type.parameter_name')
    class Meta:
        model = parts_models.ComponentParameter
        fields = '__all__'

class ComponentDistributorNumSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()
    class Meta:
        model = parts_models.DistributorNum
        fields = '__all__'

class ComponentSerializer(serializers.HyperlinkedModelSerializer):
    package_data = PackageSerializerNoLink(source='package', read_only=True)
    ro_manufacturer_name = serializers.ReadOnlyField(source='manufacturer.name')
    ro_image = serializers.ReadOnlyField(source='get_resolved_image')
    ro_component_type = serializers.ReadOnlyField(source='component_type.class_name')
    ro_parameters = ComponentParameterSerializer(many=True, source='componentparameter_set', read_only=True)
    ro_distributor_numbers = ComponentDistributorNumSerializer(many=True, source='distributornum_set', read_only=True)

    class Meta:
        model = parts_models.Component
        fields = ['url',
                  'id',
                  'name',
                  'package_data',
                  'package',
                  'pref_distri',
                  'image',
                  'manufacturer',
                  'component_type',
                  'ro_manufacturer_name',
                  'ro_image',
                  'ro_component_type',
                  'ro_parameters',
                  'ro_distributor_numbers']

class StockSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()
    ro_package_name = serializers.ReadOnlyField(source='component.package.name')
    ro_component_name = serializers.ReadOnlyField(source='component.name')
    ro_manufacturer_name = serializers.ReadOnlyField(source='component.manufacturer.name')
    ro_image = serializers.ReadOnlyField(source='component.get_resolved_image')
    class Meta:
        model = parts_models.Stock
        fields = '__all__'

class StockSerializerExpandComponent(StockSerializer):
    ro_component = ComponentSerializer(read_only=True, source='component')

class StorageSerializer(serializers.HyperlinkedModelSerializer):
    full_path = serializers.ReadOnlyField(source='get_full_path')

    class Meta:
        model = parts_models.Storage
        fields = ['url', 'id', 'name', 'verbose_name', 'parent_storage', 'responsible', 'template', 'full_path']

class StorageSerializerStocksExpanded(StorageSerializer):
    ro_stocks = StockSerializerExpandComponent(many=True, read_only=True, source='stock_set')

    class Meta(StorageSerializer.Meta):
        fields = StorageSerializer.Meta.fields + ['ro_stocks']

class DistributorSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()
    class Meta:
        model = parts_models.Distributor
        fields = '__all__'

class StockIncrementDecrementSerializer(serializers.Serializer):
	increment = serializers.IntegerField()

class ManufacturerSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()
    class Meta:
        model = parts_models.Manufacturer
        fields = '__all__'

class ComponentTypeSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()
    class Meta:
        model = parts_models.ComponentType
        fields = '__all__'

class ComponentParameterTypeSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.ReadOnlyField()
    descriptive_name = serializers.ReadOnlyField()
    class Meta:
        model = parts_models.ComponentParameterType
        fields = '__all__'