"""
首页测试模块
测试首页的各项功能和特性
"""

import pytest
from typing import Dict, Any

from pages.home_page import HomePage


class TestHomePage:
    """首页测试类"""
    
    @pytest.mark.smoke
    @pytest.mark.navigation
    def test_home_page_loads_successfully(self, home_page: HomePage):
        """测试首页正常加载"""
        home_page.navigate()
        home_page.verify_page_loaded()
    
    @pytest.mark.smoke
    def test_home_page_title(self, home_page: HomePage):
        """测试首页标题"""
        home_page.navigate()
        home_page.assert_title_contains("睿新致远")
    
    @pytest.mark.smoke
    def test_home_page_url(self, home_page: HomePage):
        """测试首页URL"""
        home_page.navigate()
        home_page.assert_url_equals(home_page._get_full_url("/"))
    
    @pytest.mark.regression
    def test_home_page_header(self, home_page: HomePage):
        """测试页头"""
        home_page.navigate()
        home_page.verify_header()
    
    @pytest.mark.regression
    def test_home_page_hero_section(self, home_page: HomePage):
        """测试Hero区域"""
        home_page.navigate()
        home_page.verify_hero_section()
    
    @pytest.mark.regression
    def test_home_page_services_section(self, home_page: HomePage):
        """测试服务区域"""
        home_page.navigate()
        home_page.verify_services_section()
    
    @pytest.mark.regression
    def test_home_page_products_section(self, home_page: HomePage):
        """测试产品区域"""
        home_page.navigate()
        home_page.verify_products_section()
    
    @pytest.mark.regression
    def test_home_page_news_section(self, home_page: HomePage):
        """测试新闻区域"""
        home_page.navigate()
        home_page.verify_news_section()
    
    @pytest.mark.regression
    def test_home_page_contact_section(self, home_page: HomePage):
        """测试联系区域"""
        home_page.navigate()
        home_page.verify_contact_section()
    
    @pytest.mark.regression
    def test_home_page_footer(self, home_page: HomePage):
        """测试页脚"""
        home_page.navigate()
        home_page.verify_footer()
    
    @pytest.mark.regression
    def test_home_page_all_sections(self, home_page: HomePage):
        """测试所有区域"""
        home_page.navigate()
        home_page.verify_all_sections()
    
    @pytest.mark.navigation
    @pytest.mark.interactive
    def test_scroll_to_about_section(self, home_page: HomePage):
        """测试滚动到关于区域"""
        home_page.navigate()
        home_page.scroll_to_section("about")
        home_page.assert_element_visible("#about", timeout=5000)
    
    @pytest.mark.navigation
    @pytest.mark.interactive
    def test_scroll_to_services_section(self, home_page: HomePage):
        """测试滚动到服务区域"""
        home_page.navigate()
        home_page.scroll_to_section("services")
        home_page.assert_element_visible("#services", timeout=5000)
    
    @pytest.mark.navigation
    @pytest.mark.interactive
    def test_scroll_to_products_section(self, home_page: HomePage):
        """测试滚动到产品区域"""
        home_page.navigate()
        home_page.scroll_to_section("products")
        home_page.assert_element_visible("#products", timeout=5000)
    
    @pytest.mark.navigation
    @pytest.mark.interactive
    def test_scroll_to_news_section(self, home_page: HomePage):
        """测试滚动到新闻区域"""
        home_page.navigate()
        home_page.scroll_to_section("news")
        home_page.assert_element_visible("#news", timeout=5000)
    
    @pytest.mark.navigation
    @pytest.mark.interactive
    def test_scroll_to_contact_section(self, home_page: HomePage):
        """测试滚动到联系区域"""
        home_page.navigate()
        home_page.scroll_to_section("contact")
        home_page.assert_element_visible("#contact", timeout=5000)
    
    @pytest.mark.performance
    def test_home_page_performance(self, home_page: HomePage):
        """测试首页性能"""
        home_page.navigate()
        performance = home_page.verify_page_performance()
        
        # 验证关键性能指标
        assert performance.get("pageLoadTime", 0) < 5000, "页面加载时间超过5秒"
        assert performance.get("domContentLoaded", 0) < 3000, "DOM内容加载时间超过3秒"
    
    @pytest.mark.performance
    def test_home_page_load_time(self, home_page: HomePage):
        """测试首页加载时间"""
        import time
        
        home_page.navigate()
        
        start_time = time.time()
        home_page.wait_for_load()
        end_time = time.time()
        
        load_time = (end_time - start_time) * 1000  # 转换为毫秒
        
        # 断言加载时间在阈值内
        assert load_time < 5000, f"首页加载时间 {load_time:.2f}ms 超过5秒阈值"
    
    @pytest.mark.responsive
    def test_home_page_mobile_layout(self, home_page: HomePage):
        """测试移动端布局"""
        home_page.verify_responsive_design(375, 667)
    
    @pytest.mark.responsive
    def test_home_page_tablet_layout(self, home_page: HomePage):
        """测试平板端布局"""
        home_page.verify_responsive_design(768, 1024)
    
    @pytest.mark.responsive
    def test_home_page_desktop_layout(self, home_page: HomePage):
        """测试桌面端布局"""
        home_page.verify_responsive_design(1920, 1080)
    
    @pytest.mark.responsive
    def test_home_page_wide_layout(self, home_page: HomePage):
        """测试宽屏布局"""
        home_page.verify_responsive_design(2560, 1440)
    
    @pytest.mark.interactive
    def test_get_company_info(self, home_page: HomePage):
        """测试获取公司信息"""
        home_page.navigate()
        info = home_page.get_company_info()
        
        assert "name" in info
        assert "slogan" in info
        assert "description" in info
    
    @pytest.mark.interactive
    def test_get_statistics(self, home_page: HomePage):
        """测试获取统计数据"""
        home_page.navigate()
        stats = home_page.get_statistics()
        
        assert "customers" in stats
        assert "cases" in stats
    
    @pytest.mark.interactive
    def test_get_featured_services(self, home_page: HomePage):
        """测试获取服务列表"""
        home_page.navigate()
        services = home_page.get_featured_services()
        
        assert isinstance(services, list)
        if len(services) > 0:
            assert "title" in services[0]
    
    @pytest.mark.interactive
    def test_get_latest_news(self, home_page: HomePage):
        """测试获取最新新闻"""
        home_page.navigate()
        news = home_page.get_latest_news()
        
        assert isinstance(news, list)
        if len(news) > 0:
            assert "title" in news[0]
    
    @pytest.mark.regression
    def test_page_refresh(self, home_page: HomePage):
        """测试页面刷新"""
        home_page.navigate()
        home_page.reload()
        home_page.verify_page_loaded()
    
    @pytest.mark.navigation
    def test_navigation_links_count(self, home_page: HomePage):
        """测试导航链接数量"""
        home_page.navigate()
        
        nav_links = home_page._find_all("nav a")
        
        # 应该有6个导航链接：首页、关于我们、核心业务、产品服务、新闻动态、联系我们
        assert len(nav_links) >= 5, f"导航链接数量不足，当前{len(nav_links)}个"
