import csv
import os
import platform


# 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, price, data, smscount, smsprice):
        self.iccid = iccid
        self.imsi = imsi
        self.price = price
        self.smscount = smscount
        self.data = data
        self.smsprice = smsprice

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


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, price=price, data=data, smscount=0, smsprice=0)
# dict_by_iccid = { iccid1: One(), iccid2:One(), iccid3:One(), }

# 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'])

        if iccid2 in dict_by_iccid:
            dict_by_iccid[iccid2].imsi = imsi2
        else:
            print(("none"))
            #dict_by_iccid[iccid2] = One(iccid=iccid2, imsi=imsi2, price=0, data=data, smscount=0, smsprice=0)

# dict_by_iccid = { iccid1: One(), iccid2:One(), iccid3:One(), }
# new dictionary for easier look up
dict_by_imsi = {}
for i in dict_by_iccid.values():
    dict_by_imsi[i.imsi] = i
# 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:
            dict_by_imsi[imsi3].smsprice += smsprice
            dict_by_imsi[imsi3].smscount += smscountnum
        else:
            dict_by_imsi[imsi3] = One(iccid=None, imsi=imsi3, price=price, data=data, smscount=smscountnum, smsprice=0)

print(dirpath+'\VFGDSP_output.csv')

with open('VFGDSP_output_'+ext_date_from_filaname,'w',newline='') as csv_file:
 writer = csv.writer(csv_file)
 writer.writerow(["ICCID", "IMSI", "PRICE", "DATA", "SMSCOUNT", "SMSPRICE"])
 for value in dict_by_imsi.values():
    print(value.iccid, value.imsi, value.price, value.data, value.smscount, value.smsprice)

    writer.writerow([value.iccid, value.imsi,value.price,value.data,value.smscount,value.smsprice])


    #print(i)

# 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])

