# -*- coding: utf-8 -*-

import datetime
import random
import time
import string

TMIN_SLEEP = 2
TMAX_SLEEP = 6

SALE_TRADE_CONDITION_NAME = "Scalability Sale Trade Condition"
PREFIX_TITLE = ""
MAX_PRODUCT = 5

def fillRelatedObjects(browser, result, name, maximum=1, actionName="", TMIN_SLEEP=0, TMAX_SLEEP=0, col_num=0, text="") :
  """
  Function to fill randomly related objetcs

  @param browser: browser
  @type browser: Browser
  @param result: result
  @type result:
  @param name: Name of the input name attribute
  @type name: string
  @param maximum: Max number of related objects
  @type maximum: int
  @param actionName: Name of the action (used for backtrace and statistic ?)
  @type actionName: string
  @param TMIN_SLEEP: Min time to sleep (in second)
  @type TMIN_SLEEP: int
  @param TMAX_SLEEP: Max time to sleep (in second)
  @type TMAX_SLEEP: int
  @param col_num: The numero of the column to filter
  @type col_num: int
  @param text: Text used to filter
  @type text: string
  """

  # Go to the section linked by the input
  result('GoTo '+actionName+' Relations', browser.mainForm.getControl(
      name=name).click())

  # Check the status
  assert (( browser.getTransitionMessage() == 'Please select one (or more) object.' )
           or ( browser.getTransitionMessage() == 'Please select one object.' ))

  # Check if it is possible to choose many objects
  if (browser.getTransitionMessage() == 'Please select one object.'):
    assert ( maximum <= 1 )

  # Filter applied the 'col_num' column with text 'text'
  if col_num != 0 :
    browser.mainForm.getListboxControl(line_number=2, column_number=col_num).value = text
    browser.mainForm.submitDialogUpdate()

  # Get the number of lines
  page_stop_number = browser.etree.xpath('//span[@class="listbox-current-page-stop-number"]')
  if len(page_stop_number) > 0:
    num_line = int(page_stop_number[0].text)
  else:
    num_line = 0
  # Choose randomly one or more objects
  if ( num_line > 0 ) and ( maximum > 0 ):
    iteration = random.randint(1, maximum)
    for i in range(0, iteration):
      line_number = random.randint(1,num_line) + 2
      # Check the box corresponding to line_number if not already checked
      #if browser.mainForm.getListboxControl(line_number=line_number, column_number=1).selected == False:
      if browser.mainForm.getListboxControl(line_number=line_number, column_number=1).value == False:
        browser.mainForm.getListboxControl(line_number=line_number, column_number=1).click()
  result('Submit '+actionName+' Relations',
      browser.mainForm.submit(name='Base_callDialogMethod:method',
                                        sleep=(TMIN_SLEEP, TMAX_SLEEP)))

  # Check whether the changes have been successfully updated
  assert browser.getTransitionMessage() == 'Data updated.'


def addOrderLine(browser, my_title, result) :
  """
  Add an order line to the sale order

  @param browser: Browser
  @type browser: Browser
  @param my_title: The sale order title
  @type my_title: string
  """
  # Create a new Sale Order Line
  browser.mainForm.submitSelectAction(label="Add Sale Order Line")
  assert browser.getTransitionMessage() == 'Object created.'

  # Fill the quantity randomly
  browser.mainForm.getControl(name='field_my_quantity').value = str(random.randint(1, 20))
  result('SetRelationProduct', browser.mainForm.submitSave(
      sleep=(TMIN_SLEEP, TMAX_SLEEP)))

  ## Choose the product randomly
  fillRelatedObjects(browser, result,
      "portal_selections/viewSearchRelatedDocumentDialog0:method", 1,
               "AddOrderLine", TMIN_SLEEP, TMAX_SLEEP)

def generateString(size) :
  """
  Function to generate a string randomly (a-z)
  @param size: Size of the string
  @type size: int
  """
  new_string = random.choice(string.ascii_uppercase)
  new_string = new_string + ''.join(random.choice(string.ascii_lowercase) for x in range(size))
  return new_string

def selectRandomOption(browser, select_name):
  """
  Function to select randomly an option

  @param browser: Browser
  @type browser: Browser
  @param select_name: Name of the input
  @type select_name: string
  """
   # Get the option values
  options = browser.mainForm.getControl(name=select_name).options[1:]
  if len(options) > 0:
    # Select randomly one value
    browser.mainForm.getControl(name=select_name).value = [random.choice(options)]

def createSaleOrder(result, browser):
  """
  Create a Sale Order with details using Sale Trade Condition to fill,
  and add some random sale order lines.
  Use the following command:
  performance_tester_erp5 http://foo.bar:4242/erp5/ 1 createSaleOrder

  Please note that  you must run this command from the  same directory of this
  script and userInfo.py.  Further information  about performance_tester_erp5
  options and arguments are available by specifying ``--help''.

  This test requires the bt5 erp5_simulation_performance_test to be installed
  for relation with organisation, also it requires a configured Sale Trade Condition.
  """
  # Open ERP5 homepage and log in
  result('Open', browser.open())

  # Log in unless already logged in by a previous test suite
  result('Login', browser.mainForm.submitLogin(
                            sleep=(TMIN_SLEEP, TMAX_SLEEP)))

  # Go to sale Order module
  result('GotoModule',
      browser.mainForm.submitSelectModule(value='/sale_order_module',
                                       sleep=(TMIN_SLEEP, TMAX_SLEEP)))

  # Create a new sale order and record the time elapsed in seconds
  result('Create',
    browser.mainForm.submitNew(sleep=(TMIN_SLEEP, TMAX_SLEEP)))

  # Check whether it has been successfully created
  assert browser.getTransitionMessage() == 'Object created.'
  my_order_sale_url = browser.url.split("?")[0]

  # Fill the title
  my_title = PREFIX_TITLE + generateString(6)
  browser.mainForm.getControl(name='field_my_title').value = my_title

  # Set some random informations
  my_str = generateString(random.randint(1,100))
  browser.mainForm.getControl(name='field_my_comment').value = my_str
  browser.mainForm.getControl(name='field_my_description').value = my_str

  # Select some options randomly
  selectRandomOption(browser, "field_my_order")

  # Set dates
  date = datetime.datetime.now()
  browser.mainForm.getControl(name='subfield_field_my_start_date_day').value = str(date.day)
  browser.mainForm.getControl(name='subfield_field_my_start_date_month').value = str(date.month)
  browser.mainForm.getControl(name='subfield_field_my_start_date_year').value = str(date.year)


  # Submit the changes, record the time elapsed in seconds
  result('Save',
    browser.mainForm.submitSave(sleep=(TMIN_SLEEP, TMAX_SLEEP)))

  # Check whether the changes have been successfully updated
  assert browser.getTransitionMessage() == 'Data updated.'
  sale_order_url = browser.url

  # Go back to the sale order page
  browser.open(sale_order_url) # test

  ## Set Sale Trade conditions
  # Click on the specified menu
  #result('GoToSaleTradeConditionRelations', browser.mainForm.getControl(
  #    name="portal_selections/viewSearchRelatedDocumentDialog2:method").click())
  #assert browser.getTransitionMessage() == 'Please select one object.'
  #line_number = browser.getListboxPosition(SALE_TRADE_CONDITION_NAME, column_number=2)
  ## Check the box corresponding to line_number
  #browser.mainForm.getListboxControl(line_number=line_number, column_number=1).click()
  #result('SubmitSaleTradeConditionRelation',
  #    browser.mainForm.submit(name='Base_callDialogMethod:method',
  #                                      sleep=(TMIN_SLEEP, TMAX_SLEEP)))

  # Add Sale order lines
  #max_ite = random.randint(1,MAX_PRODUCT)
  #for i in range (0, max_ite):
  #  browser.open(sale_order_url+"/view")
  #  addOrderLine(browser, my_title, result)

  addOrderLine(browser, my_title, result)
  browser.open(sale_order_url)

  # Validate the Sale Order
  browser.mainForm.submitSelectWorkflow(value='plan_action')
  result('Validate',
      browser.mainForm.submitDialogConfirm(sleep=(TMIN_SLEEP, TMAX_SLEEP)))
  #assert browser.getTransitionMessage() == 'Status changed.'
  # XXX Test case incomplete
