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, me_page, diaryposts_detailpage_Page,questiondetailpage_Page
from Charles.mitm import mitm_query
from appiumConfigure import backtoapp
from Charles import AssertLen
import time
import json


class Case(appiumConfig):

    def setUp(self):
        super().setUp()
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(5)
        # 点击我的按钮
        y = self.driver.get_window_size()['height']
        x = self.driver.get_window_size()['width']
        print(x, y)
        self.driver.tap([[int(x - 10), int(y - 10)]])
        self.driver.tap([[900, 520]])
        # 点击回答tab
        ele = find_element.element(self.driver, me_page.answertab_click)
        ele.click()
        # 点击回答列表跳转详情页
        ele = find_element.element(self.driver, me_page.answerlist_click)
        ele.click()
        # 有高亮词引导就点击掉
        find_element.ElementNotAssert(self.driver, diaryposts_detailpage_Page.highlight_word)
        time.sleep(1)
        # 点击查看全部回答按钮
        self.driver.tap([[352, 441]])



    def test01_questiondetailpage(self):
        '问题列表页pv事件'
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "message_id": "",
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer",
        #         "answer_detail"
        #     ],
        #     "in": "1629945531",
        #     "out_time_millis": "1629945537367",
        #     "referrer": "answer_detail",
        #     "fake": 0,
        #     "page_name": "question_answer_detail",
        #     "referrer_id": "711257",
        #     "business_id": "230287",
        #     "out": "1629945537",
        #     "referrer_tab_name": "",
        #     "in_time_millis": "1629945531762",
        #     "extra_param": "",
        #     "is_first": 1,
        #     "is_push": 0
        # },
        # "type": "page_view",
        result = mitm_query(action="page_view", referrer="answer_detail", page_name="question_answer_detail")
        AssertLen.maidiannumber(result)




    def test_02_questiondetailpage(self):
        '返回按钮埋点点击'
        ele = find_element.element(self.driver,questiondetailpage_Page.cannel)
        ele.click()
        # "params": {
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer"
        #     ],
        #     "button_name": "return",
        #     "page_name": "question_answer_detail"
        # },
        # "type": "on_click_button",
        result = mitm_query(action="on_click_button", button_name="return", page_name="question_answer_detail")
        AssertLen.maidiannumber(result)




    def test_03_questiondetailpage(self):
        '搜索按钮点击'
        ele = find_element.element(self.driver,questiondetailpage_Page.search)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "page_name": "question_answer_detail",
        #     "tab": "",
        #     "referrer": "answer_detail",
        #     "input_type": "详情页默认词",
        #     "business_id": "230287",
        #     "query": "美白针",
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer",
        #         "answer_detail"
        #     ],
        #     "referrer_id": "711257"
        # },
        # "type": "do_search",
        result = mitm_query(action="do_search", page_name="question_answer_detail",referrer="answer_detail", input_type="详情页默认词")
        AssertLen.maidiannumber(result)



    def test_04_questiondetailpage(self):
        '收藏按钮点击'
        ele = find_element.element(self.driver,questiondetailpage_Page.collection)
        ele.click()
        time.sleep(2)
        ele = find_element.element(self.driver, questiondetailpage_Page.collection)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "business_id": "230287",
        #     "page_name": "question_answer_detail",
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer",
        #         "answer_detail"
        #     ],
        #     "button_name": "attention",
        #     "referrer": "answer_detail",
        #     "referrer_id": "711257"
        # },
        # "type": "on_click_button",
        result = mitm_query(action="on_click_button", page_name="question_answer_detail",button_name="attention")
        AssertLen.maidiannumber(result)


    def test_05_questiondetailpage(self):
        '取消收藏按钮点击'
        ele = find_element.element(self.driver,questiondetailpage_Page.collection)
        ele.click()
        time.sleep(2)
        ele = find_element.element(self.driver, questiondetailpage_Page.collection)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "business_id": "230287",
        #     "page_name": "question_answer_detail",
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer",
        #         "answer_detail"
        #     ],
        #     "button_name": "no_attention",
        #     "referrer": "answer_detail",
        #     "referrer_id": "711257"
        # },
        # "type": "on_click_button",
        result = mitm_query(action="on_click_button", page_name="question_answer_detail",button_name="no_attention")
        AssertLen.maidiannumber(result)



    def test_06_questiondetailpage(self):
        '分享按钮点击'
        ele = find_element.element(self.driver,questiondetailpage_Page.share)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "page_name": "question_answer_detail",
        #     "business_id": "230287",
        #     "referrer": "answer_detail",
        #     "referrer_tab_name": "",
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer",
        #         "answer_detail"
        #     ],
        #     "referrer_id": "711257",
        #     "tab_name": "",
        #     "url": "https:\/\/m.igengmei.com\/question\/230287?is_share=1"
        # },
        # "type": "page_click_share",
        result = mitm_query(action="page_click_share", page_name="question_answer_detail")
        AssertLen.maidiannumber(result)


    def test_07_questiondetailpage(self):
        '关注按钮点击'
        self.driver.tap([[939,690]])
        time.sleep(2)
        self.driver.tap([[939, 690]])
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "motion": "do",
        #     "attention_type": "user",
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer",
        #         "answer_detail"
        #     ],
        #     "page_name": "question_answer_detail",
        #     "business_id": "230287"
        # },
        # "type": "on_click_attention",
        result = mitm_query(action="on_click_attention", motion="do", page_name="question_answer_detail")
        AssertLen.maidiannumber(result)



    def test_08_questiondetailpage(self):
        '关注按钮点击'
        self.driver.tap([[939,690]])
        time.sleep(2)
        self.driver.tap([[939, 690]])
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "motion": "undo",
        #     "attention_type": "user",
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer",
        #         "answer_detail"
        #     ],
        #     "page_name": "question_answer_detail",
        #     "business_id": "230287"
        # },
        # "type": "on_click_attention",
        result = mitm_query(action="on_click_attention", motion="undo", page_name="question_answer_detail")
        AssertLen.maidiannumber(result)



    def test_09_questiondetailpage(self):
        '我来回答按钮点击'
        self.driver.tap([[582,2122]])
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_link": [
        #
        #     ],
        #     "button_name": "answer",
        #     "business_id": "230287",
        #     "page_name": "question_answer_detail"
        # },
        # "type": "on_click_button",
        result = mitm_query(action="on_click_button", button_name="answer", page_name="question_answer_detail")
        AssertLen.maidiannumber(result)


    def test_10_questiondetailpage(self):
        '回答页面pv事件'
        self.driver.tap([[582, 2122]])
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "message_id": "",
        #     "referrer_link": [
        #         "home",
        #         "personal_home",
        #         "my_favor_answer",
        #         "answer_detail",
        #         "question_answer_detail"
        #     ],
        #     "in": "1629962804",
        #     "out_time_millis": "1629962809333",
        #     "referrer": "question_answer_detail",
        #     "fake": 0,
        #     "page_name": "create_answer",
        #     "referrer_id": "230287",
        #     "business_id": "",
        #     "out": "1629962809",
        #     "referrer_tab_name": "",
        #     "in_time_millis": "1629962804182",
        #     "extra_param": "",
        #     "is_first": 0,
        #     "is_push": 0
        # },
        # "type": "page_view",
        result = mitm_query(action="page_view", page_name="create_answer", referrer="question_answer_detail")
        AssertLen.maidiannumber(result)















































