# from web3 import Web3
# import time

# # web3 = Web3(Web3.HTTPProvider("https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"))
# web3 = Web3(Web3.HTTPProvider("https://rpc-mumbai.maticvigil.com/"))

# # abi ='[ { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deletePPTConversion", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "indexed": false, "internalType": "string", "name": "project", "type": "string" }, { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "Subtitle", "type": "event" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadPPTConversionData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getPPTConversion", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct PPTconversionDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" } ]'
# # 0x6bdE12132A7C290cBF4d0110904062fC5D827840
# # CAddress = '0x6bdE12132A7C290cBF4d0110904062fC5D827840'
# CAddress = '0x74336F39f17beb560C908523BffD1C3A59D9A9B5'
# abi ='[ { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "indexed": false, "internalType": "string", "name": "project", "type": "string" }, { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "conversion", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "pubkey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deletePPTConversion", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "pubKey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadPPTConversionData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getPPTConversion", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct PPTConversionDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" } ]'
# contractInst = web3.eth.contract(address=CAddress, abi=abi)

def UploadPPTConversionData(privatekey,pubkey,user_id,project,data): 
    try:
        print("private Key",privatekey)
        # acc1 = web3.eth.account.from_key(privatekey).address
        # nonce = web3.eth.getTransactionCount(acc1)
        # uploadData = contractInst.functions.UploadPPTConversionData(pubkey,user_id,project,data).buildTransaction({
        # 'gasPrice': web3.eth.gas_price, 'chainId': 80001, 'from': acc1, 'nonce': nonce})
        # signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey)
        # transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
        # print("transaction hash",transaction_hash)
        # transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
        # tx_id = transaction_hash.hex()
        # return tx_id
    except Exception as e:
        # print({"error":str(e)})
        return e

def getPPTConversion(private_key,user_id,project):
    # ACCOUNT = web3.eth.account.from_key(private_key).address
    try:
        print("hello")
        # getData = contractInst.functions.getPPTConversion(user_id,project).call({'from':ACCOUNT})
        # return True ,getData
    except Exception as e:
        print("Error calling function:", e)
        return False,e
    
def getUserprojectIds(user_id):
    try:
        print("hello")
        # getProjectId = contractInst.functions. getProjectId(user_id).call()
        # return getProjectId
    except Exception as e:
        return e  

def deletePPTConversion(privatekey, pubkey,user_id,project): 
    try:
        print("hello")
        # acc1 = web3.eth.account.from_key(privatekey).address
        # nonce = web3.eth.getTransactionCount(acc1)
        # deleteData = contractInst.functions.deletePPTConversion(pubkey,user_id,project).buildTransaction({
        # 'gasPrice': web3.eth.gas_price, 'chainId': 80001, 'from': acc1, 'nonce': nonce})
        # signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey)
        # transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
        # transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
        # tx_id = transaction_hash.hex()
        # return tx_id
    except Exception as e:
        print({"PPT_conversion_delete_error":str(e)})

# deletePPTConversion

# UploadPPTConversionData("0x6f3ec6c92c7f6ad1e6b8db6c3666e55bed39034b933534c68b35467c3197e272",00,1234,"pravesh yadav")
# deletePPTConversion("0x6f3ec6c92c7f6ad1e6b8db6c3666e55bed39034b933534c68b35467c3197e272",00,1234)