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

from starlord.ocr.api import *
import requests
import selenium
from selenium import webdriver
import json, urllib,urllib2
import hashlib
from urllib import urlencode
from selenium.webdriver.common import keys as KEYS
import bs4
import sys
import time
from selenium.webdriver.common.action_chains import ActionChains
from PIL import Image as PILImage
import cv2
from PIL import Image
import random



def extractEdges(image_file):

    edges = []
    img = cv2.imread(image_file, 0)
    gray_lap = cv2.Laplacian(img,cv2.CV_16S,ksize = 3)
    dst = cv2.convertScaleAbs(gray_lap)
    cv2.imwrite("verify2.png",dst)

    #cv2.imshow("showimage", dst)
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()


    image = Image.open("verify2.png")
    image_rgb = image.convert("RGB")

    for x in xrange(2, image_rgb.size[0] - 1):
        for y in xrange(2, image_rgb.size[1] - 1):
            color1 = image_rgb.getpixel((x,y))
            #白色
            if color1==(255,255,255):

                k = min(y+22,image.size[1] - 1)
                allwhite = False
                for j in xrange(y+1,k):
                    #余下竖线为白色

                    color2= image_rgb.getpixel((x,j))
                    if color2==color1:
                        allwhite = True
                        continue
                    else:
                        allwhite=False
                        break

                if allwhite:
                    if edges.count(x)==0:
                        edges.append(x)


    for i in xrange(0,len(edges)-1):
        if edges[i]+1==edges[i+1]:
            edges[i]=0

    for x in edges:
        if x==0:
            edges.remove(x)

    for z in edges:
        print str(z)

    if len(edges)==2:
        distance1 = edges[1]-edges[0]
    elif len(edges)>2:
        distance1 = edges[2]-edges[0]

    return distance1


headers0 = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Connection': 'keep-alive',
    'Accept-Encoding': 'gzip,deflate',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Cache-Control': 'max-age=0'
}

driver = webdriver.Chrome()

driver.maximize_window()

page = driver.get("http://www.qixin.com/login")


#elem = driver.find_element_by_xpath("//script[6]")

time.sleep(2)

#elem = driver.find_element_by_xpath("//div[@class='behavior_verify_content']")

elem = driver.find_element_by_css_selector('.gt_slider_knob.gt_show')

ActionChains(driver).click_and_hold(elem).perform()

time.sleep(1)

driver.get_screenshot_as_file('web.png')



#print elem.location.values()

elem2 = driver.find_element_by_css_selector('.gt_cut_fullbg.gt_show')

#ActionChains(driver).move_to_element(elem).perform()
#driver.get_screenshot_as_file('2.png')

#print elem2.location.values()
#print elem2.size.values()


topx = elem2.location.values()[1]
topy = elem2.location.values()[0]

botx = topx + elem2.size.values()[0]
boty = topy + elem2.size.values()[1]

box=(topx, topy, botx, boty)

image1 = PILImage.open('web.png')

image1.crop(box).save('verify.png')
image1.close()

distance = extractEdges("verify.png")

ActionChains(driver).move_to_element(elem)

#ActionChains(driver).drag_and_drop_by_offset(elem,distance,0).perform()

road = 0


for seconds in xrange(0,20):

    if seconds==19:
        bias = distance-road
        ActionChains(driver).move_by_offset(bias, 0).perform()
    else:
        ActionChains(driver).move_by_offset(0.05 * distance, 0).perform()

    road = road + 0.05*distance

    time.sleep(1*random.random())


#ActionChains(driver).move_to_element_with_offset(elem,distance, 0).perform()

driver.get_screenshot_as_file('web2.png')

ActionChains(driver).release(elem)

time.sleep(10)













