import time

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By


class KuaiShouComments:

    # 初始化appium
    def __init__(self):
        self.desired_caps = {
            "platformName": "Android",
            "deviceName": "AQQB9X3211W00486",
            "appPackage": "com.smile.gifmaker",
            "appActivity": "com.yxcorp.gifshow.HomeActivity",
            "noReset": True,
            "automationName": "UiAutomator2"
        }
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps)
        self.driver.implicitly_wait(10)  # 设置隐式等待

        # 基本操作，关闭广告，关闭青少年模式，直播类型等等

    # 通过关键词去搜索，获取视频列表
    def search_by_keywords(self, keyword):
        # 搜索 找到搜索按钮并且点击
        search_button = self.driver.find_element(By.XPATH, r'//android.widget.ImageView[@content-desc="查找"]')
        if search_button:
            search_button.click()
            time.sleep(2)
        else:
            print("can not find search button")
            return
        # 找到搜索栏，并且输入keywords
        search_bar = self.driver.find_element(By.ID, r'com.smile.gifmaker:id/editor')
        if search_bar:
            search_bar.send_keys(keyword)
            self.driver.find_element(By.ID, r'com.smile.gifmaker:id/right_tv').click()
            time.sleep(10)
        else:
            print("fails in input keywords")
            return
        # 找到搜索结果的视频区
        sp_bar = self.driver.find_element(By.XPATH,
                                          r'//android.widget.HorizontalScrollView[@resource-id="com.smile.gifmaker:id/tabs"]/android.widget.LinearLayout/android.view.View[1]')
        if sp_bar:
            sp_bar.click()
            time.sleep(2)
        else:
            return
        # 点击第一个视频
        first_video = self.driver.find_element(By.XPATH,
                                               r'//android.widget.RelativeLayout[@resource-id="com.smile.gifmaker:id/container"][1]')
        if first_video:
            first_video.click()
        else:
            return

    # 打开视频的评论区
    def open_comments_area(self):
        comment_button = self.driver.find_element(By.ID, r'com.smile.gifmaker:id/comment_button')
        if comment_button:
            comment_button.click()
            # 把评论区最大化
            self.driver.find_element(By.ID, r'com.smile.gifmaker:id/tabs_panel_full').click()
        else:
            return
        # 点开评论区的人的头像, 暂时未配置翻页（之后需要配置翻页）
        people_list = self.driver.find_elements(By.ID, r'com.smile.gifmaker:id/avatar')
        print(len(people_list))
        for person_ele in people_list:
            self.process_comment_person(person_ele)

        # 关掉评论区
        self.driver.find_element(By.ID, r'com.smile.gifmaker:id/tabs_panel_close').click()

    # 点开头像
    def process_comment_person(self, element):
        element.click()
        time.sleep(1)
        person_bar_list = self.driver.find_elements(By.ID, r'com.smile.gifmaker:id/tab_text')
        if person_bar_list:
            for tab in person_bar_list:
                print(tab.text)
                if "收藏" in tab.text:
                    tab.click()
                    # 获取video_list
                    self.driver.find_element(By.ID, r'com.smile.gifmaker:id/player_cover_container').click()
                    video_count = int(tab.text.split(" ")[1])
                    self.find_video_basic_info()
                    for i in range(10):
                        time.sleep(4)
                        self.scroll_down()
                        try:
                            self.find_video_basic_info()
                        except:
                            pass
                    self.driver.find_element(By.ID, r'com.smile.gifmaker:id/left_btn').click()
        else:
            return

        # 退出个人详情页面
        self.driver.find_element(By.ID, r'com.smile.gifmaker:id/left_btn').click()
        time.sleep(10)

    # 向下滑动
    def scroll_down(self):
        width = self.driver.get_window_size()['width']
        height = self.driver.get_window_size()['height']
        action = TouchAction(self.driver)
        action.press(x=int(0.5 * width), y=int(0.75 * height))
        action.wait(ms=300)
        action.move_to(x=int(0.5 * width), y=int(0.25 * height))
        action.release()
        action.perform()

    def find_video_basic_info(self):
        author_name = self.driver.find_element(By.ID, r'com.smile.gifmaker:id/user_name_text_view').text
        title = self.driver.find_element(By.ID, r'com.smile.gifmaker:id/element_caption_label').text
        print(author_name)
        print(title)


if __name__ == "__main__":
    ksc = KuaiShouComments()
    ksc.search_by_keywords("黄瓜不能和什么一起吃？")
    ksc.open_comments_area()
    for i in range(10):
        time.sleep(3)
        ksc.scroll_down()
        ksc.open_comments_area()
