import json

import PySAM.Pvwattsv8 as pv
import PySAM.Grid as gr
import PySAM.Utilityrate5 as ur
import PySAM.Singleowner as so

# modules required for the PVWatts - Single Owner configuration
# see https://nrel-pysam.readthedocs.io/en/master/Configs.html
system_model = pv.new()
grid_model = gr.from_existing( system_model )
rate_model = ur.from_existing( system_model )
financial_model = so.from_existing(system_model )

# list of modules in the correct execution order
modules = [ system_model, grid_model, rate_model, financial_model ]

# list of JSON input files exported from SAM for Case 1
# see https://nrel-pysam.readthedocs.io/en/master/Import.html
input_files = ['pvwattsv8.json', 'grid.json', 'utilityrate5.json','singleowner.json']

sam_case_name = 'untitled'

# loop through the JSON input files to set inputs for all modules
# assume json files are in same folder as this script
for f, m in zip(input_files, modules):
    with open( sam_case_name + '_' + f, 'r') as file:
        data = json.load(file)
        for k, v in data.items():
            if k != 'number_inputs':
                m.value(k, v)

# run the modules in the correct order
for m in modules:
    m.execute()

print('Results based on inputs from json files, should match SAM:')
print('System Capacity = ', system_model.SystemDesign.system_capacity,
      '| Annual Energy =', system_model.Outputs.ac_annual,
      '| NPV =', financial_model.Outputs.project_return_aftertax_npv)
print()

# change system capacity without recalculating total installed cost
capacity = 33000 # kWdc
system_model.SystemDesign.system_capacity = capacity

# run the modules in the correct order
for m in modules:
    m.execute()

print('Results after changing system capacity without recalculating total installed cost, NPV does not match SAM:')
print('System Capacity = ', system_model.SystemDesign.system_capacity,
      '| Annual Energy =', system_model.Outputs.ac_annual,
      '| NPV =', financial_model.Outputs.project_return_aftertax_npv)
print()

# total installed cost depends on system capacity, so need to recalculate
# it each time we change the system capacity
cost = 1.03 # $/Wdc based on default cost in SAM

# change system capacity and recalculate total installed cost
capacity = 33000 # kWdc
system_model.SystemDesign.system_capacity = capacity
financial_model.SystemCosts.total_installed_cost = capacity * cost * 1000

# run the modules in the correct order
for m in modules:
    m.execute()

print('Results after setting construction financing and debt fee to zero and recalculating total installed cost, should match SAM:')
print('System Capacity = ', system_model.SystemDesign.system_capacity,
      '| Annual Energy =', system_model.Outputs.ac_annual,
      '| NPV =', financial_model.Outputs.project_return_aftertax_npv)

# change system capacity and recalculate total installed cost
capacity = 50000 # kWdc
system_model.SystemDesign.system_capacity = capacity
financial_model.SystemCosts.total_installed_cost = capacity * cost * 1000

# run the modules in the correct order
for m in modules:
    m.execute()

# NPV should match SAM
print('System Capacity = ', system_model.SystemDesign.system_capacity,
      '| Annual Energy =', system_model.Outputs.ac_annual,
      '| NPV =', financial_model.Outputs.project_return_aftertax_npv)
