# -*- coding: utf-8 -*-
import dash_core_components as dcc
import dash_html_components as html
import dash
from datetime import datetime as dt
import importlib
import percy
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import multiprocessing
import sys
import unittest
import os
from urlparse import urlparse

from .IntegrationTests import IntegrationTests
from .utils import assert_clean_console, invincible, wait_for

# Download geckodriver: https://github.com/mozilla/geckodriver/releases
# And add to path:
# export PATH=$PATH:/Users/chriddyp/Repos/dash-stuff/dash-integration-tests
#
# Uses percy.io for automated screenshot tests
# export PERCY_PROJECT=plotly/dash-integration-tests
# export PERCY_TOKEN=...


class Tests(IntegrationTests):
    def setUp(self):
        def wait_for_element_by_id(id):
            wait_for(lambda: None is not invincible(
                lambda: self.driver.find_element_by_id(id)
            ))
            return self.driver.find_element_by_id(id)
        self.wait_for_element_by_id = wait_for_element_by_id

        def wait_for_element_by_css_selector(css_selector):
            wait_for(lambda: None is not invincible(
                lambda: self.driver.find_element_by_css_selector(css_selector)
            ))
            return self.driver.find_element_by_css_selector(css_selector)
        self.wait_for_element_by_css_selector = wait_for_element_by_css_selector


    def test_integration(self):
        app = dash.Dash(__name__)

        app.layout = html.Div([
            html.Div(id='waitfor'),
            html.Label('Dropdown'),
            dcc.Dropdown(
                options=[
                    {'label': 'New York City', 'value': 'NYC'},
                    {'label': u'Montréal', 'value': 'MTL'},
                    {'label': 'San Francisco', 'value': 'SF'}
                ],
                value='MTL'
            ),

            html.Label('Multi-Select Dropdown'),
            dcc.Dropdown(
                options=[
                    {'label': 'New York City', 'value': 'NYC'},
                    {'label': u'Montréal', 'value': 'MTL'},
                    {'label': 'San Francisco', 'value': 'SF'}
                ],
                value=['MTL', 'SF'],
                multi=True
            ),

            html.Label('Radio Items'),
            dcc.RadioItems(
                options=[
                    {'label': 'New York City', 'value': 'NYC'},
                    {'label': u'Montréal', 'value': 'MTL'},
                    {'label': 'San Francisco', 'value': 'SF'}
                ],
                value='MTL'
            ),

            html.Label('Checkboxes'),
            dcc.Checklist(
                options=[
                    {'label': 'New York City', 'value': 'NYC'},
                    {'label': u'Montréal', 'value': 'MTL'},
                    {'label': 'San Francisco', 'value': 'SF'}
                ],
                values=['MTL', 'SF']
            ),

            html.Label('Text Input'),
            dcc.Input(value='MTL', type='text'),

            html.Label('Slider'),
            dcc.Slider(
                min=0,
                max=9,
                marks={i: 'Label {}'.format(i) if i == 1 else str(i) for i in range(1, 6)},
                value=5,
            ),

            html.Label('Graph'),
            dcc.Graph(
                id='graph',
                figure={
                    'data': [{
                        'x': [1, 2, 3],
                        'y': [4, 1, 4]
                    }]
                }
            ),

            html.Label('DatePickerSingle'),
            dcc.DatePickerSingle(
                id='date-picker-single',
                date=dt(1997, 5, 10)
            ),

            html.Label('DatePickerRange'),
            dcc.DatePickerRange(
                id='date-picker-range',
                start_date=dt(1997, 5, 3),
                end_date_placeholder_text='Select a date!'
            ),

            html.Label('TextArea'),
            dcc.Textarea(
                placeholder='Enter a value...',
                style={'width': '100%'}
            ),

            html.Label('Markdown'),
            dcc.Markdown('''
                #### Dash and Markdown

                Dash supports [Markdown](http://commonmark.org/help).

                Markdown is a simple way to write and format text.
                It includes a syntax for things like **bold text** and *italics*,
                [links](http://commonmark.org/help), inline `code` snippets, lists,
                quotes, and more.
            '''.replace('    ', ''))
        ])
        self.startServer(app)

        try:
            self.wait_for_element_by_id('waitfor')
        except Exception as e:
            print(self.wait_for_element_by_id(
                '_dash-app-content').get_attribute('innerHTML'))
            raise e

        if 'PERCY_PROJECT' in os.environ and 'PERCY_TOKEN' in os.environ:
            python_version = sys.version.split(' ')[0]
            print('Percy Snapshot {}'.format(python_version))
            self.percy_runner.snapshot()

@@ -1853,32 +1853,86 @@
 , 'value': 'SF'%7D
+,%0A                    %7B'label': u'%E5%8C%97%E4%BA%AC', 'value': u'%E5%8C%97%E4%BA%AC'%7D
 %0A               
@@ -1954,32 +1954,65 @@
      value='MTL'
+,%0A                id=%7B'dropdown'%7D
 %0A            ),%0A
@@ -2290,32 +2290,86 @@
 , 'value': 'SF'%7D
+,%0A                    %7B'label': u'%E5%8C%97%E4%BA%AC', 'value': u'%E5%8C%97%E4%BA%AC'%7D
 %0A               
@@ -2722,32 +2722,86 @@
 , 'value': 'SF'%7D
+,%0A                    %7B'label': u'%E5%8C%97%E4%BA%AC', 'value': u'%E5%8C%97%E4%BA%AC'%7D
 %0A               
@@ -3124,16 +3124,70 @@
 ': 'SF'%7D
+,%0A                    %7B'label': u'%E5%8C%97%E4%BA%AC', 'value': u'%E5%8C%97%E4%BA%AC'%7D
 %0A       
@@ -3817,16 +3817,110 @@
       %7D%5D
+,%0A                    'layout': %7B%0A                        'title': u'%E5%8C%97%E4%BA%AC'%0A                    %7D
 %0A       
@@ -4459,16 +4459,19 @@
 value...
+ %E5%8C%97%E4%BA%AC
 ',%0A     
@@ -4963,16 +4963,36 @@
 nd more.
+%0A%0A                %E5%8C%97%E4%BA%AC
 %0A       

add chinese characters to tests