import argparse
import lxml
import lxml.etree as ET
import sys
import codecs
import os

import html
import re

def lreplace(pattern, sub, string):
    """
    Replaces 'pattern' in 'string' with 'sub' if 'pattern' starts 'string'.
    """
    return re.sub('^%s' % pattern, sub, string)

def rreplace(pattern, sub, string):
    """
    Replaces 'pattern' in 'string' with 'sub' if 'pattern' ends 'string'.
    """
    return re.sub('%s$' % pattern, sub, string)

def FT2ST(segment):
    segmenttagsimple=segment
    segmenttagsimple=re.sub('(<[^>]+?/>)', "<t/>",segmenttagsimple)
    segmenttagsimple=re.sub('(</[^>]+?>)', "</t>",segmenttagsimple)
    segmenttagsimple=re.sub('(<[^/>]+?>)', "<t>",segmenttagsimple)
    return(segmenttagsimple)

def FT2NT(segment):
    segmentnotags=re.sub('(<[^>]+>)', " ",segment)
    segmentnotags=' '.join(segmentnotags.split()).strip()
    return(segmentnotags)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='MTUOC program for converting all the TMX files in a given directory into a tab text.')
    parser.add_argument('-d','--dir', action="store", dest="inputdir", help='The input directory where the TMX files are located.',required=True)
    parser.add_argument('-o','--out', action="store", dest="outputfile", help='The output text file.',required=True)
    parser.add_argument('-s','--sl', nargs='+', action="store", dest="slcode", help='The codes for the source language.',required=False)
    parser.add_argument('-t','--tl', nargs='+', action="store", dest="tlcode", help='The codes for the target language.',required=False)
    parser.add_argument('--noTags', action='store_true', default=False, dest='noTags',help='Removes the internal tags.')
    parser.add_argument('--simpleTags', action='store_true', default=False, dest='simpleTags',help='Replaces tags with <t>, </t> or <t/>.')
    parser.add_argument('--noEntities', action='store_true', default=False, dest='noEntities',help='Replaces html/xml entities by corresponding characters.')

    args = parser.parse_args()
    direntrada=args.inputdir
    fsortida=args.outputfile
    sortida=codecs.open(fsortida,"w",encoding="utf-8")
    l1=args.slcode
    l2=args.tlcode

    parser = ET.XMLParser(recover=True)
    for root, dirs, files in os.walk(direntrada):
        for file in files:
            if file.endswith(".tmx"):
                try:
                    fentrada=os.path.join(root, file)
                    print(fentrada)

                    tree = ET.parse(fentrada, parser=parser)
                    rootT = tree.getroot()

                    for tu in rootT.iter('tu'):
                        sl_text=""
                        tl_text=""
                        for tuv in tu.iter('tuv'):
                            lang=tuv.attrib['{http://www.w3.org/XML/1998/namespace}lang']
                            for seg in tuv.iter('seg'):
                                try:
                                    text=ET.tostring(seg).decode("'utf-8").strip()
                                    text=lreplace("<seg>","",text)
                                    text=rreplace("</seg>","",text)
                                    if args.noEntities:
                                        text=html.unescape(text)
                                    if args.simpleTags:
                                        text=FT2ST(text)
                                    if args.noTags:
                                        text=FT2NT(text)
                                    if lang in l1: sl_text=text.replace("\n"," ")
                                    elif lang in l2: tl_text=text.replace("\n"," ")
                                except:
                                    sl_text=""
                                    tl_text=""

                        if not sl_text=="" and not tl_text=="":
                            try:
                                cadena=sl_text+"\t"+tl_text
                                sortida.write(cadena+"\n")
                            except:
                                pass
                except:
                    pass


import requests
url = 'https://freetp.org/' # url для второй страницы
r = requests.get(url)
with open('test.html', 'wb') as output_file:
  output_file.write(r.text.encode('UTF-16'))

from bs4 import BeautifulSoup

results = []
# Beautiful Soup
soup = BeautifulSoup(r.text)
film_list = soup.find('div', {'id': 'dle-content'})
counter=0
items = film_list.find_all('div', {'class': 'base'})
for item in items:
  # print(item.find('div', {'class': 'heading'}).find('a').innerHTML )
  game = item.find('div', {'class': 'heading'}).find('a').get('href')
  game_name = item.find('div', {'class': 'heading'}).find('a').text
  desc = item.find('div', {'class': 'maincont bnews'}).find('p').text
  category = item.find('p', {'class': 'lcol argcat'}).text
  counter+=1
  results.append({
      'id': counter,
      'game_name':game_name,
      'game_link': game,
      'desc': desc,
      'category': category,
  })

# print(film_list)
# print(results)
for item in results:
  print(item,'\n')


from django.shortcuts import render, get_object_or_404, redirect
from django.http import JsonResponse
from django.core import serializers
from django.views.generic import ListView, DetailView, CreateView
from django.db.models import Sum, Count, Avg, Q, Count, Case, When, F, Func
from django.db.models.functions import TruncMonth, Extract
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.models import User
from django.views.generic import (
    ListView,
    DetailView,
    CreateView,
    UpdateView,
    DeleteView,
    FormView,
)
from django.urls import reverse_lazy

# Import users views
from users import views as user_views

# Models created
from .models import BillPaid, Carrier, Bill, Product

# Forms created
from .forms import NewBillForm, NewCarrierForm, NewProductForm, UpdateBillForm

# Class to have output rounded, used for averages
class Round(Func):
    function = "ROUND"
    template = "%(function)s(%(expressions)s, 2)"

"""
# Class to have month-year
class MonthYear(Func):
    function1 = "MONTH"
    function2 = "YEAR"
    template = "%(function1)s(%(expressions)s) - %(function2)s(%(expressions)s)"
"""

# Homepage view.
def homepage(request):
    # Shows any unpaid bills:
    unpaid = (
        BillPaid.objects.all()
        .filter(paidBool=0)
        .values("paidID", "billID", "totalPaid", "notes")
    )

    # Shows summed amount for total due
    unpaid_total = (
        BillPaid.objects.all().filter(paidBool=0).aggregate(grandTotal=Sum("totalPaid"))
    )
    # unpaid_total = 10
    # Show previous 5 bills
    paid = BillPaid.objects.all().select_related("billID").order_by("-paidDate")[:5]

    # Group by, average each carrier type
    averages = (
        Bill.objects.select_related("carrierID")
        .values("carrierID")
        .annotate(avgCarrierAmount=Round(Avg(F("charge") + F("anc_fees") + F("taxes"))))
        .order_by("carrierID")
        .values("carrierID", "avgCarrierAmount")
    )

    carriers = (
        Carrier.objects.values("carrierId", "carrierName")
        .annotate(carrierID=F("carrierId"))
        .values("carrierID", "carrierName")
    )

    averages_dict = list(averages)
    carriers_dict = list(carriers)

    # Creates a list of dictionaries which combines the Averages and Carriers queries
    combined = []
    for i in range(len(averages_dict)):
        combined.append(averages_dict[i] | carriers_dict[i])
        print(combined)

    # Total average amount
    averages_total = (
        Bill.objects.values("carrierID")
        .annotate(avgCarrierAmount=Round(Avg(F("charge") + F("anc_fees") + F("taxes"))))
        .aggregate(Sum("avgCarrierAmount"))
    )

    context = {
        "unpaid": unpaid,
        "paid": paid,
        "unpaid_total": unpaid_total,
        "averages": averages,
        "averages_total": averages_total,
        "combinedAvg": combined,
    }
    return render(request, "bills/home.html", context=context)

# Average bill view
def AverageBillPaidView(request):
    # Get the running average billed amount for all bills
    runningAvg = (
        Bill.objects.values("carrierID")
        .annotate(avgCarrierAmount=Round(Avg(F("charge") + F("anc_fees") + F("taxes"))))
        .aggregate(totalavgCarrierAmount=Sum("avgCarrierAmount"))
    )

    # We want to get the average cost for each carrier/utility, by year and month
    monthlyAvg = (
        Bill.objects.select_related("related bill", "related carrier")
        .prefetch_related("related bill", "related carrier")
        .annotate(month=TruncMonth("dueDate"))
        .values("month", "carrierID_id")
        .annotate(
            avgCarrierAmount=Round(
                Avg(F("charge") + F("credit") + F("anc_fees") + F("charge"))
            )
        )
        .values("month", "avgCarrierAmount", "carrierID_id")
        .order_by("month", "carrierID_id")
    )

    carriers = (
        Carrier.objects.values("carrierId", "carrierName")
        .annotate(carrierID=F("carrierId"))
        .values("carrierID", "carrierName")
    )

    monthlyAvg_dict = list(monthlyAvg)
    carriers_dict = list(carriers)

    # Creates a list of dictionaries which combines the Averages and Carriers queries
    monthlyAvg = []
    for i in range(len(monthlyAvg_dict)):
        monthlyAvg.append(monthlyAvg_dict[i] | carriers_dict[i % len(carriers_dict)])

    # Combines the two items we want to show in view
    context = {"runningAvg": runningAvg, "monthlyAvg": monthlyAvg}

    return render(
        request=request, template_name="bills/bills-avg.html", context=context
    )

# Monthly Breakdown view
# Create a view that summarizes the bills table
def MonthlyBreakdownListView(request):
    # Will return a grouped breakdown for each month
    context = (
        BillPaid.objects.annotate(month=TruncMonth("paidDate"))
        .values("month")
        .annotate(sums=Sum("totalPaid"))
        .values("month", "sums")
        .order_by("month")
    )

    return render(
        request=request, template_name="bills/bills-mb.html", context={"mb": context}
    )

""" Adding New Bills, Carriers, Products, etc. """

class BillCreateView(LoginRequiredMixin, CreateView):
    form_class = NewBillForm
    template_name = "bills/bill-form.html"

    login_url = "/login/"
    redirect_field_name = "redirect_to"

    success_url = reverse_lazy("bills/home.html")

    def form_valid(self, form):
        return super().form_valid(form)

class CarrierCreateView(LoginRequiredMixin, CreateView):
    form_class = NewCarrierForm
    template_name = "bills/carrier-form.html"

    login_url = "/login/"
    redirect_field_name = "redirect_to"

    success_url = reverse_lazy("bills/home.html")

    def form_valid(self, form):
        return super().form_valid(form)

class ProductCreateView(LoginRequiredMixin, CreateView):
    form_class = NewProductForm
    template_name = "bills/product-form.html"

    login_url = "/login/"
    redirect_field_name = "redirect_to"

    success_url = reverse_lazy("bills/home.html")

    def form_valid(self, form):
        return super().form_valid(form)

""" Listing all available bills in order """

class BillListView(ListView):
    model = BillPaid
    # format: <app>/<model>_<viewtype>.html
    template_name = "bills/BillPaid_listview.html"
    context_object_name = "bills"
    ordering = ["-paidDate"]

""" Paying unpaid bills """

class paidBillDetailView(LoginRequiredMixin, DetailView):
    model = BillPaid
    fields = "__all__"

class paidBillUpdateView(LoginRequiredMixin, UpdateView):
    form_class = UpdateBillForm
    template_name = "bills/bill-form.html"
    login_url = "/login/"
    model = BillPaid
    success_url = reverse_lazy("bills/home.html")

    login_url = "/login/"
    redirect_field_name = "redirect_to"

    def form_valid(self, form):
        return super().form_valid(form)