from django.http import HttpResponse
from django.db.models import Count, Q

from LiveStock.models import Rancher, LiveStock, LiveStockProduct



def update_quota_rancher_threading():
    rancher=Rancher.objects.filter(trash=False).only('trash').order_by('id')
    for rancher in rancher:
        rancher.save()


def get_live_stock_info(request):
    rancher=Rancher.objects.filter(trash=False,has_script=False).only('cow','sheep','goat','horse','camel','light_livestock','heavy_livestock','has_script'
                                                                      )
    rachers=rancher.values_list('herd_code',flat=True).distinct()
    live_stocks = LiveStock.objects.filter(trash=False,herd_code__in=rachers ).only('type')
    for r in rancher:
        live_stock=live_stocks.filter(herd_code=r.herd_code).only('type')
        if live_stock:
            sheep=live_stock.filter(type='گوسفند').count()
            goat=live_stock.filter(type='بز').count()
            cow=live_stock.filter(type='گاو').count()
            horse=live_stock.filter(type='اسب').count()
            camel=live_stock.filter(type='شتر').count()
            light_livestock = live_stock.filter(type__in=('بز', 'گوسفند')).count()
            heavy_livestock = live_stock.filter(type__in=('گاو','اسب','شتر')).count()
            product = LiveStockProduct.objects.filter(trash=False, name='سبوس').first()

            r.sheep=sheep
            r.horse=horse
            r.camel=camel
            r.light_livestock=light_livestock
            r.heavy_livestock=heavy_livestock
            r.goat=goat
            r.cow=cow
            r.has_script=True
            r.weight_quota_heavy = product.heavy_wight * heavy_livestock
            r.weight_quota_light = product.light_wight * light_livestock
            r.save()

    return HttpResponse('ok')


def update_rancher(rancherss):
    for ranchers in rancherss:
        live_stocks = LiveStock.objects.filter(trash=False, herd_code=ranchers.herd_code).only('type')

        sheep = live_stocks.filter(type='گوسفند').count()
        goat = live_stocks.filter(type='بز').count()
        cow = live_stocks.filter(type='گاو').count()
        horse = live_stocks.filter(type='اسب').count()
        camel = live_stocks.filter(type='شتر').count()
        light_livestock = live_stocks.filter(type__in=('بز', 'گوسفند')).count()
        heavy_livestock = live_stocks.filter(type__in=('گاو', 'اسب', 'شتر')).count()
        product = LiveStockProduct.objects.filter(name='سبوس', trash=False).first()

        ranchers.sheep = sheep
        ranchers.horse = horse
        ranchers.camel = camel
        ranchers.light_livestock = light_livestock
        ranchers.heavy_livestock = heavy_livestock
        ranchers.goat = goat
        ranchers.cow = cow
        ranchers.weight_quota_heavy = product.heavy_wight * heavy_livestock
        ranchers.weight_quota_light = product.light_wight * light_livestock
        ranchers.save()
    return True


def al_get_live_stock_info(request):
    rancher = Rancher.objects.filter(trash=False, has_script=False).only('cow', 'sheep', 'goat', 'horse', 'camel',
                                                                         'light_livestock', 'heavy_livestock',
                                                                         'has_script'
                                                                         ).count()
    return HttpResponse(rancher)


def update_one_rancher(ranchers):
    counts = LiveStock.objects.filter(
        trash=False,
        herd_code=ranchers.herd_code
    ).aggregate(
        sheep_count=Count('id', filter=Q(type='گوسفند')),
        goat_count=Count('id', filter=Q(type='بز')),
        cow_count=Count('id', filter=Q(type='گاو')),
        horse_count=Count('id', filter=Q(type='اسب')),
        camel_count=Count('id', filter=Q(type='شتر')),
        light_count=Count('id', filter=Q(type__in=('بز', 'گوسفند'))),
        heavy_count=Count('id', filter=Q(type__in=('گاو', 'اسب', 'شتر')))
    )

    product = LiveStockProduct.objects.filter(name='سبوس', trash=False).first()

    ranchers.sheep = counts['sheep_count']
    ranchers.goat = counts['goat_count']
    ranchers.cow = counts['cow_count']
    ranchers.horse = counts['horse_count']
    ranchers.camel = counts['camel_count']
    ranchers.light_livestock = counts['light_count']
    ranchers.heavy_livestock = counts['heavy_count']

    ranchers.weight_quota_heavy = product.heavy_wight * counts['heavy_count']
    ranchers.weight_quota_light = product.light_wight * counts['light_count']

    ranchers.has_script = True
    ranchers.save()

    return True
