import 'react-native-gesture-handler';

import React, { createContext, useContext, useRef, useState, useEffect } from 'react';
import { Platform, StyleSheet, View, Alert } from 'react-native';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { MMKV } from 'react-native-mmkv';
import { WebView } from 'react-native-webview';

import { GoogleSignin } from '@react-native-google-signin/google-signin';
import { appleAuth } from '@invertase/react-native-apple-authentication';
import DeviceInfo from 'react-native-device-info';
import { AUTH_CONFIG } from '@/config/auth';


import ApplicationNavigator from '@/navigation/Application';
import { ThemeProvider } from '@/theme';
import '@/translations';

import * as RNIap from 'react-native-iap';
import type { Purchase, PurchaseError } from 'react-native-iap';
import { eventTypeEnum } from './utils/h5';

const itemSubs = ['com.recipemuse.vip.monthly']; // 月度会员订阅ID


// 订阅相关消息常量
const SUBSCRIPTION_REQUEST = 'NATIVE_SUBSCRIPTION_REQUEST';
const RESTORE_PURCHASE_REQUEST = 'NATIVE_RESTORE_PURCHASE_REQUEST';

export const queryClient = new QueryClient({
  defaultOptions: {
    mutations: {
      retry: false,
    },
    queries: {
      retry: false,
    },
  },
});

export const storage = new MMKV();

// WebView预加载上下文
interface WebViewContextType {
  webViewRef: React.RefObject<WebView | null>;
  showWebView: boolean;
  setShowWebView: (show: boolean) => void;
}

const WebViewContext = createContext<WebViewContextType | null>(null);

export const useWebViewContext = () => {
  const context = useContext(WebViewContext);
  if (!context) {
    throw new Error('useWebViewContext must be used within WebViewProvider');
  }
  return context;
};

// 配置常量
// const DEFAULT_URL = 'http://localhost:5173';

const DEFAULT_URL = 'http://10.15.49.43:5173';


// WebView用户代理
const USER_AGENT = Platform.select({
  ios: 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
  android: 'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36',
});

function App() {
  const webViewRef = useRef<WebView>(null);
  const [showWebView, setShowWebView] = useState(false);
  const [isIAPInitialized, setIsIAPInitialized] = useState(false);
  const [availableProducts, setAvailableProducts] = useState<any[]>([]);
  const [isSubscribed, setIsSubscribed] = useState(false);



  /**
   * 发送消息到 WebView
   */
  const sendMessageToWebView = (webViewRef: React.RefObject<WebView | null>, message: any) => {
    if (webViewRef.current) {
      const messageString = typeof message === 'string' ? message : JSON.stringify(message);
      // 使用更可靠的通信方式
      webViewRef.current.postMessage(messageString);
    }
  };

  /**
   * 处理来自 WebView 的消息
   */
  const handleWebViewMessage = (
    event: any,
    webViewRef: React.RefObject<WebView | null>,
  ) => {
    try {
      const messageData = event.nativeEvent.data;
      const { type, data } = messageData ? JSON.parse(messageData) : {};
      console.log('Received message:', type, data);
      
      // 检查是否为登录请求
      if (type === eventTypeEnum.NATIVE_LOGIN_REQUEST) {
        // 延迟执行登录以确保 WebView 已完全显示
        handleLogin()
      } else if (type === SUBSCRIPTION_REQUEST) {
         // 检查是否为订阅请求
        console.log('收到订阅请求');
        handleInAppPurchase()
          .then(() => {
            console.log('订阅流程已启动');
          })
          .catch((error) => {
            console.error('启动订阅失败:', error);
            sendMessageToWebView(webViewRef, {
              type: 'SUBSCRIPTION_ERROR',
            });
          });
      }
      // 检查是否为恢复购买请求
      else if (type === RESTORE_PURCHASE_REQUEST) {
        console.log('收到恢复购买请求');
        restorePurchases()
          .then(() => {
            console.log('恢复购买流程已完成');
          })
          .catch((error) => {
            console.error('恢复购买失败:', error);
            sendMessageToWebView(webViewRef, {
              type: 'RESTORE_PURCHASE_ERROR',
              status: 'error',
              message: error.message || '恢复购买失败'
            });
          });
      }
    } catch (error) {
      console.error('处理 WebView 消息时出错:', error);
    }
  };

  const handleLogin = async () => {
    try {
      const deviceId = await DeviceInfo.getUniqueId();
      console.log('设备ID:', deviceId);


      if (Platform.OS === 'ios') {
        // iOS使用Apple登录
        await handleAppleLogin();
      } else {
        // Android使用Google登录
        await handleGoogleLogin();
      }
      
    } catch (error) {
      console.error('登录失败:', error);
      Alert.alert('登录失败', '请稍后重试');
    }
  };


  const handleAppleLogin = async () => {
    try {
      // 执行Apple登录请求
      const appleAuthRequestResponse = await appleAuth.performRequest({
        requestedOperation: appleAuth.Operation.LOGIN,
        requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
      });

      // 获取凭证状态
      const credentialState = await appleAuth.getCredentialStateForUser(
        appleAuthRequestResponse.user,
      );
      
      console.log('凭证状态:', credentialState, '(0=REVOKED, 1=AUTHORIZED, 2=NOT_FOUND)');

      // 如果performRequest成功返回，说明登录已完成
      // 模拟器中credentialState可能为0(REVOKED)，但不影响登录流程
      console.log('appleAuthRequestResponse.user',appleAuthRequestResponse.user)
      debugger
      if (appleAuthRequestResponse.user) {
        debugger
        console.log('Apple登录成功:', appleAuthRequestResponse);
        
        // 仅在真机且状态为REVOKED时警告
        if (credentialState === appleAuth.State.REVOKED && !__DEV__) {
          console.warn('⚠️ 凭证状态为REVOKED，但登录已完成');
        }
        sendMessageToWebView(webViewRef, {
          type: eventTypeEnum.H5_LOGIN_SUCCESS_IOS,
          message: '登录成功',
          data: appleAuthRequestResponse
        })
      } else {
        console.log('未获取到用户信息');
        // throw new Error('未获取到用户信息');
      }
    } catch (error) {
      console.error('Apple登录失败:', error);
      // throw error;
    }
  };

  const handleGoogleLogin = async () => {
    try {
      // 检查是否有Google Play Services
      const hasPlayServices = await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: false });
      
      if (!hasPlayServices) {
        console.log('Google Play Services不可用，使用Mock数据');
        // Mock登录数据（仅用于开发测试）
        const mockUserInfo = {
          data: {
            user: {
              id: 'mock_user_123',
              name: '测试用户',
              email: 'test@example.com',
              photo: null,
            },
          },
        };
        Alert.alert('开发模式', '当前使用Mock登录数据（Google服务不可用）');
        sendMessageToWebView(webViewRef, {
          type: eventTypeEnum.H5_LOGIN_SUCCESS_ANDROID,
          status: 'success',
          message: '登录成功（Mock模式）',
          data: mockUserInfo,
        });
        return;
      }

      // 配置Google登录
      await GoogleSignin.configure({
        webClientId: AUTH_CONFIG.GOOGLE.WEB_CLIENT_ID,
        offlineAccess: true,
      });
      
      // 执行登录
      const userInfo = await GoogleSignin.signIn();
      console.log('Google登录成功:', userInfo);
      Alert.alert('登录成功', `欢迎 ${userInfo.data?.user.name || '用户'}`);  
      sendMessageToWebView(webViewRef, {
        type: 'LOGIN_SUCCESS_ANDROID',
        status: 'success',
        message: '登录成功',
        data: userInfo
      })
    } catch (error) {
      console.error('Google登录失败:', error);
      throw error;
    }
  };

  /**
   * 初始化IAP
   */
  // useEffect(() => {
  //   // 检查Google Play Services后再初始化IAP
  //   checkAndInitIAP();
  //   return () => {
  //     // 清理IAP连接
  //     if (isIAPInitialized) {
  //       RNIap.endConnection();
  //     }
  //   };
  // }, []);

  /**
   * 检查Google服务并初始化IAP
   */
  const checkAndInitIAP = async () => {
    try {
      // Android平台检查Google Play Services
      Alert.alert('Platform.OS ', Platform.OS);
      if (Platform.OS === 'android') {
        const hasPlayServices = await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: false });
        if (!hasPlayServices) {
          console.log('⚠️ Google Play Services不可用，IAP功能将被禁用');
          console.log('💡 建议：使用Android模拟器或配置系统代理进行测试');
          Alert.alert(
            '提示',
            'Google Play服务不可用\n\n建议使用以下方式测试：\n1. Android Studio模拟器（带Google Play）\n2. 真机配置系统代理\n3. 使用Mock模式开发',
            [{ text: '知道了' }]
          );
          return;
        }
      }
 
      // 有Google服务才初始化IAP
      await initIAP();
    } catch (error) {
      console.log('Google服务检查失败，跳过IAP初始化:', error);
    }
  };

  /**
   * 初始化应用内购买
   */
  const initIAP = async () => {
    try {
      console.log('🚀 开始初始化IAP...');
      console.log('📱 平台:', Platform.OS);
      
      await RNIap.initConnection();
      console.log('✅ IAP连接初始化成功');
      setIsIAPInitialized(true);

      // 获取可用的订阅产品
      const products = await RNIap.fetchProducts({ skus: itemSubs, type: 'subs' });
      console.log('可用订阅产品:', products);
      setAvailableProducts(products || []);

      // 检查现有订阅状态
      await checkSubscriptionStatus();

      // 监听购买更新
      const purchaseUpdateSubscription = RNIap.purchaseUpdatedListener(
        async (purchase: Purchase) => {
          console.log('购买更新:', purchase);
          console.log('购买状态:', purchase.purchaseState);
          console.log('平台:', purchase.platform);
          
          const receipt = purchase.purchaseToken || purchase.transactionId;
          
          if (receipt) {
            try {
              // 验证收据（这里应该发送到你的后端服务器验证）
              const isValid = await validateReceipt(receipt, purchase);
              
              if (!isValid) {
                Alert.alert('订阅失败', '收据验证失败，请联系客服');
                return;
              }

              // 完成交易 - iOS和Android处理方式不同
              if (Platform.OS === 'ios') {
                // iOS: 完成交易，订阅类产品不消耗
                await RNIap.finishTransaction({ purchase, isConsumable: false });
                console.log('iOS交易已完成');
              } else if (Platform.OS === 'android') {
                // Android: 需要确认购买（acknowledge）
                if (purchase.purchaseToken) {
                  await RNIap.acknowledgePurchaseAndroid(purchase.purchaseToken);
                  console.log('Android购买已确认');
                }
              }
              
              // 更新订阅状态
              setIsSubscribed(true);
              
              Alert.alert('订阅成功', '感谢您订阅会员服务！');
              
              // 发送订阅成功消息到WebView
              sendMessageToWebView(webViewRef, {
                type: 'SUBSCRIPTION_SUCCESS',
                status: 'success',
                message: '订阅成功',
                data: {
                  productId: purchase.productId,
                  transactionId: purchase.transactionId,
                  purchaseToken: purchase.purchaseToken,
                  platform: purchase.platform,
                  transactionDate: purchase.transactionDate,
                }
              });
            } catch (error: any) {
              console.error('验证收据失败:', error);
              Alert.alert('订阅失败', `验证收据时出错: ${error.message || '未知错误'}`);
            }
          }
        },
      );

      // 监听购买错误
      const purchaseErrorSubscription = RNIap.purchaseErrorListener(
        (error: PurchaseError) => {
          console.error('购买错误:', error);
          if (error.code !== RNIap.ErrorCode.UserCancelled) {
            Alert.alert('购买失败', error.message || '请稍后重试');
          }
        },
      );

      // 保存订阅以便清理
      return () => {
        purchaseUpdateSubscription.remove();
        purchaseErrorSubscription.remove();
      };
    } catch (error: any) {
      debugger
      console.error('❌ IAP初始化失败 - 详细信息:');
      console.error('错误对象:', error);
      console.error('错误消息:', error.message);
      console.error('错误代码:', error.code);
      console.error('错误栈:', error.stack);
      
      let errorMessage = '无法连接到应用商店';
      
      if (error.message) {
        if (error.message.includes('BILLING_UNAVAILABLE')) {
          errorMessage = 'Google Play Billing服务不可用\n\n可能原因：\n1. 模拟器未安装Google Play\n2. 未登录Google账号\n3. 国内网络限制';
        } else if (error.message.includes('SERVICE_DISCONNECTED')) {
          errorMessage = 'Google Play服务连接断开\n请检查网络和代理配置';
        } else if (error.message.includes('SERVICE_UNAVAILABLE')) {
          errorMessage = 'Google Play服务暂时不可用\n请稍后重试';
        } else {
          errorMessage = `连接失败: ${error.message}`;
        }
      }
      
      Alert.alert(
        'IAP初始化失败', 
        errorMessage + '\n\n技术信息: ' + (error.code || '未知错误'),
        [{ text: '知道了' }]
      );
    }
  };

  /**
   * 验证收据（示例实现，实际应该发送到后端服务器）
   */
  const validateReceipt = async (
    receipt: string,
    purchase: Purchase,
  ) => {
    // TODO: 将收据发送到你的后端服务器进行验证
    // 示例代码：
    // const response = await fetch('https://your-api.com/validate-receipt', {
    //   method: 'POST',
    //   headers: { 'Content-Type': 'application/json' },
    //   body: JSON.stringify({ receipt, productId: purchase.productId }),
    // });
    // const result = await response.json();
    // return result.valid;
    
    console.log('验证收据:', receipt);
    console.log('购买信息:', purchase);
    return true;
  };

  /**
   * 处理应用内购买 - 购买月度会员订阅
   */
  const handleInAppPurchase = async () => {
    if (!isIAPInitialized) {
      Alert.alert('提示', '应用商店连接未就绪，请稍后重试');
      return;
    }

    // 检查是否已订阅
    if (isSubscribed) {
      Alert.alert('提示', '您已经是会员了');
      return;
    }

    try {
      // 构建购买请求参数
      const purchaseRequest: any = {
        type: 'subs',
        request: {
          ios: {
            sku: itemSubs[0],
          },
          android: {
            skus: [itemSubs[0]],
          },
        },
      };

      // Android需要提供subscriptionOffers（从产品信息中获取）
      if (Platform.OS === 'android' && availableProducts.length > 0) {
        const product = availableProducts.find(p => p.id === itemSubs[0]);
        if (product?.subscriptionOfferDetailsAndroid?.[0]) {
          const offer = product.subscriptionOfferDetailsAndroid[0];
          purchaseRequest.request.android.subscriptionOffers = [
            {
              sku: itemSubs[0],
              offerToken: offer.offerToken,
            },
          ];
          console.log('Android订阅offer:', offer);
        } else {
          console.warn('未找到Android订阅offer，将尝试不带offer购买');
        }
      }

      console.log('发起购买请求:', purchaseRequest);
      
      // 请求购买订阅
      await RNIap.requestPurchase(purchaseRequest);
    } catch (error: any) {
      console.error('购买订阅失败:', error);
      
      // 区分不同的错误类型
      if (error.code === RNIap.ErrorCode.UserCancelled) {
        console.log('用户取消购买');
      } else if (error.code === RNIap.ErrorCode.AlreadyOwned) {
        Alert.alert('提示', '您已经拥有此订阅');
        setIsSubscribed(true);
      } else if (error.code === RNIap.ErrorCode.ItemUnavailable) {
        Alert.alert('购买失败', '此商品暂时不可用，请稍后重试');
      } else {
        Alert.alert('购买失败', error.message || '请稍后重试');
      }
    }
  };

  /**
   * 恢复购买
   */
  const restorePurchases = async () => {
    try {
      // iOS调用系统恢复购买API
      if (Platform.OS === 'ios') {
        await RNIap.restorePurchases();
        console.log('iOS恢复购买请求已发送');
      }
      
      // 获取可用购买记录
      const availablePurchases = await RNIap.getAvailablePurchases();
      console.log('可恢复的购买:', availablePurchases);

      if (availablePurchases.length === 0) {
        Alert.alert('提示', '没有找到可恢复的购买记录');
        return;
      }

      let hasSubscription = false;
      
      // 处理恢复的购买
      for (const purchase of availablePurchases) {
        if (itemSubs.includes(purchase.productId)) {
          hasSubscription = true;
          
          // 验证并激活订阅
          const receipt = purchase.purchaseToken || purchase.transactionId || '';
          const isValid = await validateReceipt(receipt, purchase);
          
          if (isValid) {
            // Android需要确认购买
            if (Platform.OS === 'android' && purchase.purchaseToken) {
              try {
                await RNIap.acknowledgePurchaseAndroid(purchase.purchaseToken);
                console.log('Android购买已确认');
              } catch (error) {
                console.log('购买可能已经被确认过:', error);
              }
            }
            
            setIsSubscribed(true);
            console.log('订阅已恢复:', purchase.productId);
          }
        }
      }

      if (hasSubscription) {
        Alert.alert('恢复成功', '您的订阅已恢复');
        
        // 发送恢复成功消息到WebView
        sendMessageToWebView(webViewRef, {
          type: 'SUBSCRIPTION_RESTORED',
          status: 'success',
          message: '订阅已恢复',
        });
      } else {
        Alert.alert('提示', '没有找到会员订阅记录');
      }
    } catch (error: any) {
      console.error('恢复购买失败:', error);
      Alert.alert('恢复失败', error.message || '无法恢复购买记录');
    }
  };

  /**
   * 检查订阅状态
   */
  const checkSubscriptionStatus = async () => {
    try {
      const availablePurchases = await RNIap.getAvailablePurchases();
      console.log('所有可用购买记录:', availablePurchases);
      
      const activeSubscription = availablePurchases.find(
        (purchase) => itemSubs.includes(purchase.productId),
      );

      if (activeSubscription) {
        console.log('当前订阅状态:', activeSubscription);
        
        // 检查订阅是否过期（Android）
        if (Platform.OS === 'android') {
          // Android的自动续订状态
          const androidPurchase = activeSubscription as any;
          const isActive = androidPurchase.autoRenewingAndroid !== false;
          setIsSubscribed(isActive);
          return isActive;
        } else {
          // iOS的订阅状态
          setIsSubscribed(true);
          return true;
        }
      }
      
      setIsSubscribed(false);
      return false;
    } catch (error) {
      console.error('检查订阅状态失败:', error);
      setIsSubscribed(false);
      return false;
    }
  };

  return (
    <GestureHandlerRootView style={styles.container}>
      <QueryClientProvider client={queryClient}>
        <ThemeProvider storage={storage}>
          <WebViewContext.Provider value={{ webViewRef, showWebView, setShowWebView }}>
            <View style={styles.container}>
              <ApplicationNavigator />
              
              {/* 预加载的WebView，通过样式控制显示/隐藏 */}
              <View style={[
                styles.webViewContainer,
                showWebView ? styles.webViewVisible : styles.webViewHidden,
                __DEV__ && showWebView ? styles.webViewDebug : null // 开发模式下添加调试样式
              ]}>
                <WebView
                  ref={webViewRef}
                  source={{ uri: DEFAULT_URL }}
                  style={styles.webView}
                  userAgent={USER_AGENT}
                  javaScriptEnabled={true}
                  domStorageEnabled={true}
                  startInLoadingState={true}
                  scalesPageToFit={true}
                  allowsBackForwardNavigationGestures={Platform.OS === 'ios'}
                  onMessage={(event) => handleWebViewMessage(event, webViewRef)}
                  onError={(error) => console.error('WebView 错误:', error)}
                  onHttpError={(error) => console.error('WebView HTTP 错误:', error)}
                />
              </View>
            </View>
          </WebViewContext.Provider>
        </ThemeProvider>
      </QueryClientProvider>
    </GestureHandlerRootView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  webViewContainer: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    zIndex: 1000,
  },
  webViewVisible: {
    opacity: 1,
  },
  webViewHidden: {
    opacity: 0,
    transform: [{ translateX: -9999 }], // 偏移到屏幕外
  },
  webView: {
    flex: 1,
  },
  // 添加一个新的样式用于调试模式
  webViewDebug: {
    top: 20,    // 留出顶部空间
    left: 10,   // 留出左侧空间
    right: 10,  // 留出右侧空间
    bottom: 20, // 留出底部空间
    borderRadius: 10, // 添加圆角以便识别
  },
});

export default App;
