import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
# import event
from appiumConfigure.config import appiumConfig
from appiumConfigure import find_element
from page import home_Page,search_Page,ai_Page,timecard_Page
from Charles.mitm import mitm_query
from appiumConfigure import backtoapp
from Charles import AssertLen
import time
from selenium.webdriver.common.action_chains import ActionChains
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.mobilecommand import MobileCommand
class Case(appiumConfig):

    # def swip(self):
    #     x = self.driver.get_window_size()['width']
    #     y = self.driver.get_window_size()['height']
    #     return x,y
    def setup(self):
        # 点击关闭弹窗
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击次卡按钮
        ele = find_element.element(self.driver, home_Page.time_card)
        ele.click()


    def test_01_time_card(self):
        '首页次卡按钮点击'
        self.setup()
        backtoapp.backapp(self.driver,2)

        # "params": {
        #     "module_id": 21,
        #     "referrer_link": "[]",
        #     "position": 0,
        #     "card_name": "更美次卡",
        #     "card_id": "21",
        #     "url": "gengmei:\/\/common_webview?url=https:\/\/backend.igengmei.com\/phantom\/visual_special\/2362",
        #     "referrer": "",
        #     "transaction_type": "operation",
        #     "referrer_id": "",
        #     "card_type": "function_entrance",
        #     "page_name": "home",
        #     "business_id": "",
        #     "absolute_position": 0
        # },
        # "type": "on_click_card",
        result = mitm_query(action="on_click_card", card_name="更美次卡", card_type="function_entrance", page_name="home")
        AssertLen.maidiannumber(result)



    def test_02_time_card(self):
        '次卡专题页面pv'
        self.setup()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "message_id": "",
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "in": "1621495597",
        #     "out_time_millis": "1621495600789",
        #     "referrer": "home",
        #     "fake": 0,
        #     "page_name": "custom_special",
        #     "referrer_id": "",
        #     "business_id": "2362",
        #     "out": "1621495600",
        #     "referrer_tab_name": "",
        #     "in_time_millis": "1621495597092",
        #     "extra_param": "",
        #     "is_first": 0,
        #     "is_push": 0
        # },
        # "type": "page_view",
        result = mitm_query(action="page_view", page_name="custom_special")
        AssertLen.maidiannumber(result)



    def test_03_time_card(self):
        '次卡顶部banner点击，进介绍页'
        self.setup()
        # 点击顶部banner 进入介绍页
        ele = find_element.element(self.driver,timecard_Page.cika_banner)
        ele.click()
        backtoapp.backapp(self.driver, 2)
        # "params": {
        #     "module_id": 25855,
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "card_id": "2315",
        #     "url": "gengmei:\/\/common_webview?url=https%3A\/\/backend.igengmei.com\/phantom\/visual_special\/2315%3Fusepage%3Dwk",
        #     "referrer": "home",
        #     "transaction_type": "operation",
        #     "referrer_id": "",
        #     "page_name": "custom_special",
        #     "position_sec": 0,
        #     "business_id": "2362",
        #     "absolute_position": 0,
        #     "card_type": "image_hotspot",
        #     "card_content_type": "45"
        # },
        # "type": "on_click_card",
        result = mitm_query(action="on_click_card", transaction_type="operation", page_name="custom_special", card_type="image_hotspot")
        AssertLen.maidiannumber(result)



    def test_04_time_card(self):
        '次卡专题页点击不同城市筛选'
        self.setup()

        ele1 = find_element.element(self.driver, timecard_Page.cika_title)
        ele2 = find_element.element(self.driver, timecard_Page.cika2)
        # 滑动
        TouchAction(self.driver).press(ele2).wait(1000).move_to(ele1).release().perform()

        # 点击重庆城市tab
        ele = find_element.element(self.driver,timecard_Page.cika_city)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "business_id": "2362",
        #     "referrer": "home",
        #
        #     "page_name": "custom_special",
        #     "position": 1,
        #     "referrer_id": ""
        # },
        # "type": "on_click_button",
        result = mitm_query(action="on_click_button", page_name="custom_special")
        AssertLen.maidiannumber(result)



    def time_card_services(self):
        self.setup()
        # 获取两个点元素
        ele1 = find_element.element(self.driver, timecard_Page.cika_title)
        ele2 = find_element.element(self.driver, timecard_Page.cika2)
        # 滑动
        TouchAction(self.driver).press(ele2).wait(1000).move_to(ele1).release().perform()
        # 根据坐标点击立即购买
        self.driver.tap([[573, 1176]])



    def test_05_time_card(self):
        '点击次卡美购-立即购买按钮'
        self.time_card_services()
        backtoapp.backapp(self.driver,2)

        # "params":{
        #     "position":0,
        #     "tab_name":"",
        #     "card_name":"复合水光(菲洛嘉3ml+润月雅2ml）  1次卡  (仅限重庆地区使用)",
        #     "sec_card_id":10148,
        #     "card_id":1003,
        #     "referrer":"home",
        #     "transaction_type":"-1",
        #     "referrer_id":"",
        #     "page_name":"custom_special",
        #     "card_type":"card",
        #     "business_id":"2362",
        #     "card_content_type":"service_self"
        # },
        # "type": "on_click_card"
        result = mitm_query(action="on_click_card", referrer="home", page_name="custom_special", card_content_type="service_self")
        AssertLen.maidiannumber(result)



    def test_06_time_card(self):
        '次卡详情页pv事件'
        self.time_card_services()
        backtoapp.backapp(self.driver, 2)
        #     "params": {
        #         "message_id": "",
        #         "in": "1621500875",
        #         "out_time_millis": "1621500882151",
        #         "referrer": "custom_special",
        #         "fake": 0,
        #         "page_name": "service_self_detail",
        #         "referrer_id": "2362",
        #         "business_id": "1003",
        #         "out": "1621500882",
        #         "referrer_tab_name": "",
        #         "in_time_millis": "1621500875520",
        #         "extra_param": "{
        #                        "service_item_id": "10148",
        #                                           "bctraceid": ""
        #     }",
        #     "is_first": 0,
        #     "is_push": 0
        #
        # },
        # "type": "page_view",
        result = mitm_query(action="page_view", referrer="custom_special", page_name="service_self_detail",)
        AssertLen.maidiannumber(result)


    def test_07_time_card(self):
        '次卡详情页点击分享按钮埋点'
        self.time_card_services()
        ele = find_element.element(self.driver,timecard_Page.cika_share)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "page_name": "service_self_detail",
        #     "business_id": "1003",
        #     "referrer": "custom_special",
        #     "button_name": "share",
        #     "referrer_link": [
        #         "home",
        #         "custom_special"
        #     ],
        #     "referrer_id": "2362",
        #     "extra_param": "{
        #                    "service_item_id": "10009",
        #                                       "bctraceid": ""
        # }"
        # },
        # "type": "on_click_button",
        result = mitm_query(action="on_click_button", page_name="service_self_detail", button_name="share")
        AssertLen.maidiannumber(result)


    def test_08_time_card(self):
        '正品保障提示点击埋点'
        self.time_card_services()
        ele = find_element.element(self.driver,timecard_Page.cika_zhengpin)
        ele.click()
        backtoapp.backapp(self.driver,2)
        #     "params": {
        #         "button_name": "service_features",
        #         "business_id": "1003",
        #         "referrer": "welfare_attribute",
        #         "page_name": "service_self_detail",
        #         "extra_param": "{
        #                        "service_item_id": "10009",
        #                                           "bctraceid": ""
        #     }",
        #     "referrer_id": "2362",
        #     "referrer_link": [
        #         "home",
        #         "custom_special",
        #         "service_self_detail",
        #         "welfare_attribute"
        #     ]
        #
        # },
        #     "type": "on_click_button",
        result = mitm_query(action="on_click_button", button_name="service_features",page_name="service_self_detail")
        AssertLen.maidiannumber(result)


    def test_09_time_card(self):
        '勾选次数按钮'
        self.time_card_services()
        cika_count = find_element.element(self.driver,timecard_Page.cika_count)
        cika_count.click()
        backtoapp.backapp(self.driver,2)
        #     "params": {
        #         "button_name": "optional_area",
        #         "business_id": "1003",
        #         "referrer": "custom_special",
        #         "page_name": "service_self_detail",
        #         "extra_param": "{
        #                        "service_item_id": "10010",
        #                                           "bctraceid": ""
        #     }",
        #     "referrer_id": "2362",
        #     "referrer_link": [
        #         "home",
        #         "custom_special"
        #     ]
        #
        # },  "type":"on_click_button",
        result = mitm_query(action="on_click_button", button_name="optional_area", referrer="custom_special", page_name="service_self_detail")
        AssertLen.maidiannumber(result)


    def test_10_time_card(self):
        'sku弹窗popup_view'
        self.time_card_services()
        cika_count = find_element.element(self.driver, timecard_Page.cika_count)
        cika_count.click()
        backtoapp.backapp(self.driver,2)
        #     "params": {
        #         "page_name": "service_self_detail",
        #         "business_id": "1003",
        #         "referrer": "custom_special",
        #         "popup_name": "sku_choose",
        #         "extra_param": "{
        #                        "service_item_id": "10009",
        #                                           "bctraceid": ""
        #     }",
        #     "referrer_id": "2362",
        #     "referrer_link": [
        #         "home",
        #         "custom_special"
        #     ]
        #
        # },   "type":"popup_view",
        result = mitm_query(action="popup_view", page_name="service_self_detail", popup_name="sku_choose")
        AssertLen.maidiannumber(result)


    # def test_11_time_card(self):
    #     '点击次卡详情页立即购买按钮'
    #     self.time_card_services()
    #     cika_count = find_element.element(self.driver, timecard_Page.cika_count)
    #     cika_count.click()
    #     time.sleep(1)
    #     # 点击立即购买按钮
    #     x = int(self.driver.get_window_size()['width'])
    #     y = int(self.driver.get_window_size()['height'])
    #
    #     print(type(x), type(y))
    #     print(x,y)
    #     for i in range(1,300):
    #         self.driver.tap([[int(x / 2), y-i*3]])
    #         print(i)
    #     time.sleep(2000)
    #     print("111")
    #     backtoapp.backapp(self.driver,2)


    def test_12_time_card(self):
        '适用机构查询点击'
        self.time_card_services()
        ele = find_element.element(self.driver,timecard_Page.cika_jigou_list_pv)
        ele.click()
        backtoapp.backapp(self.driver,2)
        #     "params": {
        #         "button_name": "organization_area",
        #         "business_id": "1003",
        #         "referrer": "custom_special",
        #         "page_name": "service_self_detail",
        #         "extra_param": "{
        #     \"service_item_id\" : \"10010\",
        #     \"bctraceid\" : \"\"
        #     }",
        #     "referrer_id": "2362",
        #     "referrer_link": [
        #         "home",
        #         "custom_special"
        #     ]
        #
        # },  "type":"on_click_button",
        result = mitm_query(action="on_click_button", button_name="organization_area", page_name="service_self_detail")
        AssertLen.maidiannumber(result)


    def test_13_time_card(self):
        '适用机构列表pv'
        self.time_card_services()
        ele = find_element.element(self.driver, timecard_Page.cika_jigou_list_pv)
        ele.click()
        backtoapp.backapp(self.driver, 2)
        # "params": {
        #     "message_id": "",
        #     "referrer_link": [
        #         "home",
        #         "custom_special",
        #         "service_self_detail"
        #     ],
        #     "in": "1621824729",
        #     "out_time_millis": "1621824738692",
        #     "referrer": "service_self_detail",
        #     "fake": 0,
        #     "page_name": "hospital_list",
        #     "referrer_id": "1003",
        #     "business_id": "",
        #     "out": "1621824738",
        #     "referrer_tab_name": "",
        #     "in_time_millis": "1621824729930",
        #     "extra_param": "",
        #     "is_first": 1,
        #     "is_push": 0
        # },   "type":"page_view",
        result = mitm_query(action="page_view", referrer="service_self_detail", page_name="hospital_list")
        AssertLen.maidiannumber(result)




    def test_14_time_card(self):
        '点击机构列表任意机构'
        self.time_card_services()
        ele = find_element.element(self.driver, timecard_Page.cika_jigou_list_pv)
        ele.click()
        ele = find_element.element(self.driver,timecard_Page.cika_jigou_list_click)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_link": [
        #         "home",
        #         "custom_special",
        #         "service_self_detail"
        #     ],
        #     "tab_name": "智能排序",
        #     "card_name": "北京凯润婷（原史三八）医疗美容医院",
        #     "position": 0,
        #     "card_id": "96ea37eef81d11e692f800163e000a4a",
        #     "referrer": "service_self_detail",
        #     "transaction_type": "-1",
        #     "referrer_id": "1003",
        #     "page_name": "hospital_list",
        #     "card_type": "card",
        #     "business_id": "",
        #     "card_content_type": "hospital"
        # },  "type":"on_click_card",
        result = mitm_query(action="on_click_card", tab_name="智能排序", referrer="service_self_detail", page_name="hospital_list", card_type="card",card_content_type="hospital")
        AssertLen.maidiannumber(result)

    def test_15_time_card(self):
        '次卡机构列表精准曝光'
        self.time_card_services()
        ele = find_element.element(self.driver, timecard_Page.cika_jigou_list_pv)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "up_loading_times": 0,
        #     "referrer_link": [
        #         "home",
        #         "custom_special",
        #         "service_self_detail"
        #     ],
        #     "down_slide_times": 0,
        #     "is_exposure": "1",
        #     "tab_name": "智能排序",
        #     "up_slide_times": 0,
        #     "referrer": "service_self_detail",
        #     "referrer_id": "1003",
        #     "page_name": "hospital_list",
        #     "business_id": "",
        #     "exposure_cards": [
        #         {
        #             "card_type": "card",
        #             "card_content_type": "hospital",
        #             "relative_position": "0",
        #             "card_name": "北京凯润婷（原史三八）医疗美容医院",
        #             "transaction_type": "-1",
        #             "in_page_pos": "",
        #             "card_id": "96ea37eef81d11e692f800163e000a4a",
        #             "absolute_position": "0"
        #         },
        #         {
        #             "card_type": "card",
        #             "card_content_type": "hospital",
        #             "relative_position": "1",
        #             "card_name": "北京煤医医疗美容医院（原煤炭总医院整形科）",
        #             "transaction_type": "-1",
        #             "in_page_pos": "",
        #             "card_id": "9cf0d6ffa4944b09b2729a22301fc570",
        #             "absolute_position": "1"
        #         },
        #         {
        #             "card_type": "card",
        #             "card_content_type": "hospital",
        #             "relative_position": "2",
        #             "card_name": "北京亚楠容悦医疗美容诊所",
        #             "transaction_type": "-1",
        #             "in_page_pos": "",
        #             "card_id": "4f6ced94b6af4c06af84a3046e533149",
        #             "absolute_position": "2"
        #         },
        #         {
        #             "card_type": "card",
        #             "card_content_type": "hospital",
        #             "relative_position": "3",
        #             "card_name": "北京和颜美帕医疗美容",
        #             "transaction_type": "-1",
        #             "in_page_pos": "",
        #             "card_id": "b6ba88115f644914ac45ecea051abc35",
        #             "absolute_position": "3"
        #         },
        #         {
        #             "card_type": "card",
        #             "card_content_type": "hospital",
        #             "relative_position": "4",
        #             "card_name": "北京欧扬医疗美容门诊部",
        #             "transaction_type": "-1",
        #             "in_page_pos": "",
        #             "card_id": "bjoyylmr",
        #             "absolute_position": "4"
        #         }
        #     ],
        #     "down_loading_times": 0
        # },   "type":"page_precise_exposure",
        result = mitm_query(action="page_precise_exposure", tab_name="智能排序",referrer="service_self_detail", page_name="hospital_list")
        AssertLen.maidiannumber(result)
        exposure_cards = result[0].get("params").get("exposure_cards")
        if len(exposure_cards) == 5:
            pass
        else:
            raise Exception('次卡机构列表精准曝光数量不对 %s' % len(exposure_cards))





    def test_16_time_card(self):
        '次卡详情页面咨询按钮点击'
        self.time_card_services()
        ele = find_element.element(self.driver,timecard_Page.cika_zixun_button)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "page_name": "service_self_detail",
        #     "business_id": "1003",
        #     "referrer": "custom_special",
        #     "button_name": "consultation",
        #     "referrer_link": [
        #         "home",
        #         "custom_special"
        #     ],
        #     "referrer_id": "2362",
        #     "extra_param": "{
        # \"service_item_id\" : \"10010\",
        # \"bctraceid\" : \"\"
        # }"
        # },  "type":"on_click_button",
        result = mitm_query(action="on_click_button", page_name="service_self_detail", referrer="custom_special", button_name="consultation")
        AssertLen.maidiannumber(result)


    def test_17_time_card(self):
        '次卡详情页点击立即购买埋点'
        self.time_card_services()
        ele = find_element.element(self.driver,timecard_Page.cika_buy_button)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "page_name": "service_self_detail",
        #     "business_id": "1006",
        #     "referrer": "custom_special",
        #     "button_name": "buy_now",
        #     "referrer_link": [
        #         "home",
        #         "custom_special"
        #     ],
        #     "referrer_id": "2362",
        #     "extra_param": "{
        # \"service_item_id\" : \"10017\",
        # \"bctraceid\" : \"\"
        # }"
        # },   "type":"on_click_button",

        result = mitm_query(action="on_click_button", page_name="service_self_detail", button_name="buy_now")
        AssertLen.maidiannumber(result)




















