import csv
import os
import platform
import sys


from discord_webhook import DiscordWebhook

webhook = DiscordWebhook(
    url='https://discordapp.com/api/webhooks/744608404848246926/FwwWUNmuWsCetFDBOcA1trCx6nSflI0yX3f9BNh7-oB5a2VYSLl882di23XK2TNWwQxP',
    username="billing vfgdsp")

# system decision
osver = platform.system()
print("Running on: ", osver)

if osver == 'Linux':
    print("Setting path to: .")
    dirpath = "."

elif osver == 'Windows':
    print("Setting path to: %userprofile%\documents")
    dirpath = "%userprofile%\documents"

else:
    print("Not covered system architecture, exiting")
    exit()

# FILE = 'Summarised_data_usage_per_ICCID_(STCU)_20211101_20211130_2021-12-02T94742084Z.CSV'
# FILE2 = 'Standard_Pairing_File___2021-12-02T1142395Z.CSV'
# FILE3 = 'VFGDSP202110_Summarised_SMS_usage_per_IMSI.CSV'


FILE = [filename for filename in os.listdir(dirpath) if filename.startswith("VFGDSP_data_")]
FILE2 = [filename for filename in os.listdir(dirpath) if filename.startswith("Standard_Pairing_File")]
FILE3 = [filename for filename in os.listdir(dirpath) if filename.startswith("VFGDSP_sms_")]

FILE = FILE[0]
FILE2 = FILE2[0]
FILE3 = FILE3[0]

ext_date_from_filaname = FILE.split('_')[-1]
print(ext_date_from_filaname)


class One(object):
    def __init__(self, iccid, imsi, msisdn, price, data, smscount, smsprice, simstatus):
        self.iccid = iccid
        self.imsi = imsi
        self.msisdn = msisdn
        self.price = price
        self.smscount = smscount
        self.data = data
        self.smsprice = smsprice
        self.simstatus = simstatus

    def __str__(self):
        return " iccid: %s, imsi: %s, msisdn: %s, price: %s, data: %s, smscount: %s, smsprice: %s, simstatus: %s)" % (
        self.iccid, self.imsi, self.msisdn, self.price, self.data, self.smscount, self.smsprice, self.simstatus)


dict_by_iccid = {}
with open(FILE, newline='', encoding="utf-8") as csvfile:
    reader = csv.DictReader(csvfile, delimiter=',')
    for row in reader:
        # print('raw row processing: ',row)
        iccid = int(row['ICCID'])
        # print(row['Sum_Total_Kbytes'])
        price = row['Sum_Total_Kbytes'].replace(',', '')

        price = price.replace(' ', '')
        price2 = price

        price = (2.23 * float(price)) / 1024
        data = float(price2) / 1024

        if iccid in dict_by_iccid:
            dict_by_iccid[iccid].price += price
            dict_by_iccid[iccid].data += data
        else:
            dict_by_iccid[iccid] = One(iccid=iccid, imsi=0, msisdn=0, price=price, data=data, smscount=0, smsprice=0,
                                       simstatus=None)
            print("creating dict for: ", iccid, " price: ", price, " data: ", data)

# dict_by_iccid = { iccid1: One(), iccid2:One(), iccid3:One(), }
iccid2counter = 0
# only add IMSI if possible

with open(FILE2, newline='', encoding="utf-8") as csvfile2:
    reader2 = csv.DictReader(csvfile2, delimiter=',')
    for row in reader2:

        imsi2 = int(row['IMSI'])
        iccid2 = int(row['ICCID'])
        msisdn = int(row['MSISDN'])
        simstate = (row['SIM State'])

        if iccid2 in dict_by_iccid:
            print("iccid: ", iccid2, " already exists, doing the math")
            # print(msisdn)
            dict_by_iccid[iccid2].imsi = imsi2
            dict_by_iccid[iccid2].msisdn = msisdn
            dict_by_iccid[iccid2].simstatus = simstate
            print(dict_by_iccid[iccid2])


        else:
            # print("not there")

            dict_by_iccid[iccid2] = One(iccid=iccid2, imsi=imsi2, msisdn=msisdn, price=0, data=0, smscount=0,
                                        smsprice=0, simstatus=simstate)
            # print("creating iccid dict")

# dict_by_iccid = { iccid1: One(), iccid2:One(), iccid3:One(), }
# new dictionary for easier look up

dict_by_imsi = {}
count = 0
for i in dict_by_iccid.values():
    dict_by_imsi[i.imsi] = i
    print("this is the dict after we convert it to imsi")
    print(dict_by_imsi[i.imsi])
#    if i.msisdn == 882390031038971:
#        print('problematic ', dict_by_imsi[i.msisdn])
#        count +=1
#        if count > 5:
#            assert False

print("counter: ", iccid2counter)

# dict_by_imsi = { imsi1: One(), imsi2:One(), imsi3:One(), }

# add price for one imsi
with open(FILE3, newline='', encoding="utf-8") as csvfile3:
    reader3 = csv.DictReader(csvfile3, delimiter=',')
    for row in reader3:
        smspay = int(row['Nr._of_SMS_payload'])
        smsmo = row['Nr._of_SMS_MO'].replace(',', '')
        smsmo = int(smsmo)
        # print(row['IMSI'])
        imsi3 = int(row['IMSI'])
        # print("IMSI: ",imsi)
        # print("smspay: ",smspay)
        # print("smsmo: ", smsmo)

        smsprice = (smspay * 0.26) + (smsmo * 1.28)
        smscountnum = smspay + smsmo

        if imsi3 in dict_by_imsi:
            # print("record found")
            dict_by_imsi[imsi3].smsprice += smsprice
            dict_by_imsi[imsi3].smscount += smscountnum

        else:
            # print("record not found")
            # TODO: TADY VZNIKA CHYBA
            dict_by_imsi[imsi3] = One(iccid=None, imsi=imsi3, msisdn=None, price=0, data=0, smscount=smscountnum,
                                      smsprice=smsprice, simstatus=None)

print(dirpath + '\VFGDSP_output.csv')

print("last section")

for value in dict_by_imsi.values():
    # print(value.iccid, value.imsi, value.msisdn, value.price, value.data, value.smscount, value.smsprice)
    if value.msisdn == 882390031038971:
        print('problematic2', value)
        # print('problematic2',value.msisdn, value.iccid, value.imsi, value.price, value.data, value.smsprice, value.smscount, "0")

with open('output_VFGDSP_' + ext_date_from_filaname, 'w', newline='') as csv_file4:
    writer = csv.writer(csv_file4)
    # writer.writerow(["ICCID", "IMSI", "MSISDN", "PRICE", "DATA", "SMSCOUNT", "SMSPRICE"])

    # Need:
    writer.writerow(
        ["MSISDN", "ICCID", "IMSI", "DATA-PRICE", "DATA-AMOUNT", "SMS-PRICE", "SMS-COUNT", "OTHER-PRICE", "SIM-STATUS"])

    otherprice = 0
    for value in dict_by_imsi.values():
        print(value)
        if value.simstatus == "Active.Live" or value.smscount != 0 or value.data != 0:
            writer.writerow(
                [value.msisdn, value.iccid, value.imsi, value.price, value.data, value.smsprice, value.smscount, "0",
                 value.simstatus])



        else:
            print("neprosla, vyhazujeme")
            print(value.msisdn, value.iccid, value.imsi, value.price, value.data, value.smsprice, value.smscount, "0",
                  value.simstatus)

csv_file4.close()


with open("output_VFGDSP_202202.csv", "rb") as f:
    webhook.add_file(file=f.read(), filename='output_VFGDSP_202202.csv')

response = webhook.execute()




# with open('dict.csv', 'w') as csv_file:
#    writer = csv.writer(csv_file)
#    for key, value in dict_by_imsi.items():
#       writer.writerow([key, value])

# ve FILE 2 musi byt 901280031055847
