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,community_Page
from Charles.mitm import mitm_query
from appiumConfigure import backtoapp
from Charles import AssertLen
from appium.webdriver.common.touch_action import TouchAction
import time
import json
from Charles.maidianRedis import redis_gm
from pprint import pprint
class Case(appiumConfig):

    def setUp(self):
        super().setUp()
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击底部社区按钮
        ele = find_element.element(self.driver, home_Page.content_click)
        ele.click()


    def test_01_community(self):
        '社区首页城市筛选器'
        ele = find_element.element(self.driver,community_Page.community_city)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "page_name": "new_community_home"
        # },   "type":"home_click_navbar_area",
        result = mitm_query(action="home_click_navbar_area", page_name="new_community_home")
        AssertLen.maidiannumber(result)

    def test_02_community(self):
        '社区搜索框点击埋点'
        #到达社区首页
        ele = find_element.element(self.driver,community_Page.community_search)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_link": [
        #
        #     ],
        #     "page_name": "new_community_home",
        #     "explicit_word_num": "1"
        # },  "type":"on_click_navbar_search",
        result = mitm_query(action="on_click_navbar_search", page_name="new_community_home")
        AssertLen.maidiannumber(result)


    def test_03_community(self):
        '社区发帖提问按钮点击'
        ele = find_element.element(self.driver,community_Page.community_post)
        ele.click()
        backtoapp.backapp(self.driver, 2)
        # "params": {
        #     "type": "new",
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "page_name": "new_community_home"
        # },  "type":"on_click_create_topic",
        result = mitm_query(action="on_click_create_topic", page_name="new_community_home")
        AssertLen.maidiannumber(result)


    def test_04_community(self):
        '社区首页pv事件'
        backtoapp.backapp(self.driver,2)
        #     "params": {
        #         "message_id": "",
        #         "referrer_link": [
        #             "home",
        #             "new_community_home",
        #             "home"
        #         ],
        #         "in": "1621995957",
        #         "out_time_millis": "1621995972961",
        #         "referrer": "home",
        #         "fake": 0,
        #         "page_name": "new_community_home",
        #         "referrer_id": "",
        #         "business_id": "",
        #         "out": "1621995972",
        #         "referrer_tab_name": "",
        #         "in_time_millis": "1621995957640",
        #         "extra_param": "{
        #     \"is_first_start\" : \"0\"
        #     }",
        #     "is_first": 0,
        #     "is_push": 0
        #
        # }, "type":"page_view",
        result = mitm_query(action="page_view", referrer="home", page_name="new_community_home")
        AssertLen.maidiannumber(result)



    def test_05_community(self):
        '更美颜究所点击时间'
        ele = find_element.element(self.driver,community_Page.community_yanjiusuo)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "page_name": "new_community_home",
        #     "business_id": "",
        #     "referrer": "home",
        #     "button_name": "join",
        #     "in_page_pos": "免费招募",
        #     "referrer_link": [
        #         "home",
        #         "new_community_home",
        #         "home"
        #     ],
        #     "referrer_id": "",
        #     "tab_name": "",
        #     "referrer_tab_name": ""
        # }, "type":"on_click_button",
        result = mitm_query(action="on_click_button", page_name="new_community_home", button_name="join", in_page_pos="免费招募",referrer="home")
        AssertLen.maidiannumber(result)




    def test_06_community(self):
        '签到好礼banner点击'
        ele = find_element.element(self.driver,community_Page.community_qiandao)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "page_name": "new_community_home",
        #     "business_id": "",
        #     "referrer": "home",
        #     "button_name": "look_detail",
        #     "in_page_pos": "签到",
        #     "referrer_link": [
        #         "home",
        #         "new_community_home",
        #         "home"
        #     ],  "type":"on_click_button",
        result = mitm_query(action="on_click_button", page_name="new_community_home", referrer="home", button_name="look_detail", in_page_pos="签到")
        AssertLen.maidiannumber(result)


    def test_07_community(self):
        '点击微整tab'
        ele = find_element.element(self.driver,community_Page.community_weizheng_tab)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "page_name": "new_community_home",
        #     "business_id": "",
        #     "referrer": "home",
        #     "position": 1,
        #     "tab_type": "in_tab",
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "referrer_id": "",
        #     "from_tab_name": "精选",
        #     "tab_name": "微整",
        #     "sec_tab_name": "",
        #     "referrer_tab_name": ""
        # },  "type":"on_click_tab",
        result = mitm_query(action="on_click_tab", page_name="new_community_home", referrer="home", tab_type="in_tab", from_tab_name="精选", tab_name="微整")
        AssertLen.maidiannumber(result)


    def test_08_community(self):
        '微整tab下的精准曝光'
        ele = find_element.element(self.driver, community_Page.community_weizheng_tab)
        ele.click()
        backtoapp.backapp(self.driver, 2)

        # "params": {
        #     "up_loading_times": 0,
        #     "referrer_link": [
        #         "home",
        #         "new_community_home"
        #     ],
        #     "down_slide_times": 0,
        #     "is_exposure": "1",
        #     "tab_name": "微整",
        #     "up_slide_times": 0,
        #     "referrer": "new_community_home",
        #     "referrer_id": "",
        #     "page_name": "new_community_home",
        #     "business_id": "",
        #     "is_popup": "0",
        #     "exposure_cards": [
        #         {
        #             "card_type": "card",
        #             "card_content_type": "diary",
        #             "is_cpc": 0,
        #             "transaction_type": "-1",
        #             "redirect_content_type": "diary",
        #             "card_style": "cascade",
        #             "card_id": "17759043",
        #             "request_id": "483CD7E0-4EFF-41A5-AC76-2863BB80504E_319101729134671667339865723062485371495",
        #             "redirect_id": 17759043,
        #             "relative_position": "0",
        #             "absolute_position": "0"
        #         },
        #         {
        #             "card_type": "card",
        #             "card_content_type": "diary",
        #             "is_cpc": 0,
        #             "transaction_type": "-1",
        #             "redirect_content_type": "diary",
        #             "card_style": "cascade",
        #             "card_id": "17147376",
        #             "request_id": "483CD7E0-4EFF-41A5-AC76-2863BB80504E_319101729134671667339865723062485371495",
        #             "redirect_id": 17147376,
        #             "relative_position": "1",
        #             "absolute_position": "1"
        #         }
        #     ],
        #     "down_loading_times": 0
        # },   "type":"page_precise_exposure",

        result= mitm_query(action="page_precise_exposure", tab_name="微整", page_name="new_community_home",referrer="home")
        AssertLen.maidiannumber(result)
        exposure_cards = result[0].get("params").get("exposure_cards")
        if len(exposure_cards) == 2:
            pass
        else:
            raise Exception('精准曝光数量与预期不一致 %s' % result)


    def test_09_community(self):
        '社区tab下的feed流卡片点击'
        ele = find_element.element(self.driver, community_Page.community_weizheng_tab)
        ele.click()
        ele = find_element.element(self.driver,community_Page.community_card_click)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "redirect_content_type": "diary",
        #     "card_id": "17759043",
        #     "position": 0,
        #     "transaction_type": "-1",
        #     "business_id": "",
        #     "referrer_tab_name": "",
        #     "is_cpc": 0,
        #     "query": "",
        #     "page_name": "new_community_home",
        #     "referrer_id": "",
        #     "request_id": "483CD7E0-4EFF-41A5-AC76-2863BB80504E_77671589738111700761615504157204141948",
        #     "card_status": "",
        #     "referrer_link": [
        #         "
        #     ],
        #     "card_type": "card",
        #     "sec_card_content_type": "",
        #     "referrer": "home",
        #     "tag_id": "",
        #     "redirect_id": 17759043,
        #     "card_but_pos": "card",
        #     "card_content_type": "diary",
        #     "tab_name": "微整",
        #     "channel_id": "",
        #     "card_style": "cascade"
        # },   "type":"on_click_card",
        result = mitm_query(action="on_click_card", redirect_content_type="diary", page_name="new_community_home", card_type="card", tab_name="微整")
        AssertLen.maidiannumber(result)


    def test_10_community(self):
        '社区首页下，反复点击tab切换 校验是否会漏掉精准曝光'
        ele = find_element.element(self.driver, community_Page.community_weizheng_tab)
        ele.click()
        ele = find_element.element(self.driver,community_Page.community_jingxuan_tab)
        ele.click()
        ele = find_element.element(self.driver, community_Page.community_weizheng_tab)
        ele.click()
        ele = find_element.element(self.driver, community_Page.community_jingxuan_tab)
        ele.click()
        backtoapp.backapp(self.driver,2)
        time.sleep(2)
        redis_gm.delete("maidian")
        ele = find_element.element(self.driver, community_Page.community_weizheng_tab)
        ele.click()
        # "params": {
        #     "up_loading_times": 0,
        #     "referrer_link": [
        #         "home",
        #         "new_community_home"
        #     ],
        #     "down_slide_times": 0,
        #     "is_exposure": "1",
        #     "tab_name": "微整",
        #     "up_slide_times": 0,
        #     "referrer": "new_community_home",
        #     "referrer_id": "",
        #     "page_name": "new_community_home",
        #     "business_id": "",
        #     "is_popup": "0",
        #     "exposure_cards": [
        #         {
        #             "card_type": "card",
        #             "card_content_type": "diary",
        #             "is_cpc": 0,
        #             "transaction_type": "-1",
        #             "redirect_content_type": "diary",
        #             "card_style": "cascade",
        #             "card_id": "17759043",
        #             "request_id": "483CD7E0-4EFF-41A5-AC76-2863BB80504E_319101729134671667339865723062485371495",
        #             "redirect_id": 17759043,
        #             "relative_position": "0",
        #             "absolute_position": "0"
        #         },
        #         {
        #             "card_type": "card",
        #             "card_content_type": "diary",
        #             "is_cpc": 0,
        #             "transaction_type": "-1",
        #             "redirect_content_type": "diary",
        #             "card_style": "cascade",
        #             "card_id": "17147376",
        #             "request_id": "483CD7E0-4EFF-41A5-AC76-2863BB80504E_319101729134671667339865723062485371495",
        #             "redirect_id": 17147376,
        #             "relative_position": "1",
        #             "absolute_position": "1"
        #         }
        #     ],
        #     "down_loading_times": 0
        # },   "type":"page_precise_exposure",

        result = mitm_query(action="page_precise_exposure", tab_name="微整", page_name="new_community_home",
                            referrer="home")
        AssertLen.maidiannumber(result)
        exposure_cards = result[0].get("params").get("exposure_cards")
        if len(exposure_cards) == 2:
            pass
        else:
            raise Exception('精准曝光数量与预期不一致 %s' % result)


