import { View, Text, ScrollView, Pressable, Button, Alert, StyleSheet } from 'react-native';
import React, { useContext } from 'react';
import { SafeAreaView } from 'react-native-safe-area-context';
import { router } from 'expo-router';
import { CrossLogoSvg, RightArrowIconSvg } from '../global/SVG';
import NormalButton from '../global/normal_button';
import { useTranslation } from '../../util/hooks/useTranslation';

type TermsSectionProps = {
  title: string| undefined;
  clauses: string[];
};

const TermsSection: React.FC<TermsSectionProps> = ({ title, clauses }) => (
  <View className="mb-3">
    {title && (
      <Text style={styles.sectionTitle}>{title}</Text>
    )}
    {clauses.map((clause, index) => (
      <Text key={index} style={styles.clause}>
        {clause}
      </Text>
    ))}
  </View>
);
const UserTermsPageComponent: React.FC = () => {
  const { getCurrentLanguageConfig, t } = useTranslation()
  const curren = getCurrentLanguageConfig()
  const sectionsZh = [
    {
      clauses: [
        '歡迎使用 Crazy Charge（以下簡稱「本公司」或「我們」）提供的電動車充電服務（以下簡稱「服務」）。請仔細閱讀以下條款及細則（以下簡稱「本條款」），使用 Crazy Charge 此應用程式即表示您同意遵守本條款的所有內容。'
      ]
    },
    {
      title: '1. 服務使用',
      clauses: [
        '1.1 用戶須透過本公司APP進行充值、付款及啟動充電服務。',
        '1.2 用戶須確保所提供的資料（包括車牌、付款資料）真實、準確及完整，並對其帳戶的一切活動負責。',
        '1.3 本公司保留隨時修改、暫停或終止服務的權利，而無須事先通知用戶。'
      ]
    },
    {
      title: '2. 充值及付款',
      clauses: [
        '2.1 用戶可透過APP進行充值，充值金額將存入用戶帳戶。',
        '2.2 充值金額沒有使用期限，用戶可保留作日後使用。',
        '2.3 本公司可能不時提供優惠（如折扣、贈額等），相關優惠受特定條款約束，詳情請參閱APP內的優惠說明。'
      ]
    },
    {
      title: '3. 退款政策',
      clauses: [
        '3.1 如用戶要求退款，須透過 WhatsApp提交申請，並由本公司審核。',
        '3.2 退款金額將扣除該次充值所涉及的所有優惠（如適用）。',
        '3.3 每筆退款將收取港幣150元作為行政費用，並從退款金額中扣除。',
        '3.4 基於行政成本及資源考慮，建議用戶將未使用的充值金額留作日後急需充電時使用，而非申請退款。',
        '3.5 退款處理時間由本公司決定，並可能受行政、銀行或支付平台影響。'
      ]
    },
    {
      title: '4. 責任限制',
      clauses: [
        '4.1 本公司將盡力確保服務的穩定性及準確性，但不保證服務不受干擾、及時、安全或無誤。',
        '4.2 用戶理解並同意，因使用本服務而導致的任何直接或間接損失（包括但不限於數據損失、時間損失或收益損失），本公司概不負責。',
        '4.3 如因不可抗力（如天災、網絡故障、政府行為等）導致服務中斷或數據損失，本公司不承擔任何責任。'
      ]
    },
    {
      title: '5. 用戶行為及責任',
      clauses: [
        '5.1 用戶的車輛完成充電後須 15分鐘內取車並離開車場，否則須繳付 $3/分鐘的違泊罰款。',
        '5.2 用戶的車輛如有任何濫用本公司的免時租政策，包括但不限於車輛停泊在我們充電站範圍而非充電中、完成充電後繼續停泊等等，本公司有權作進一步行動，包括但不限於鎖車（開鎖費 $2,000）；計時罰款 $3/分鐘等等。',
        `5.3 用戶不得從事任何違反香港法律或影響服務正常運作的行為，包括但不限於:
  - 未經授權存取他人帳戶； 
  - 利用系統漏洞或進行任何形式的欺詐行為； 
  - 干擾、破壞或過度負荷本公司的伺服器或網絡。`,
        '5.4 倘若因用戶的不當或不合理使用服務，或妨礙其他用戶正常使用服務而造成的任何損失或損害，用戶必須承擔由此所產生的所有索償、要求、訴訟及其他法律責任。',
        '5.5 如客戶車輛於充電服務完結或並未使用充電服務而將車輛停泊或駛出/駛入本公司範圍內，一律視為違規停泊，本公司有權收取罰款或將車輛拖走，車輛如有任何直接或間接損失，本公司概不負責，亦不會承擔任何停車場責任。',
        '5.6 任何違反用戶條款的用戶，如在充電站範圍內發生任何意外或損失，本公司概不負責。',
        '5.7 本公司保留權利，追討因用戶行為導致本公司、服務使用者或者其他第三方的損失。',
        '5.8 如用戶違反本條款，本公司有權暫停或終止其帳戶，並保留追究法律責任的權利。'
      ]
    },
    {
      title: '6. 隱私政策',
      clauses: [
        '6.1 本公司將按照《個人資料（私隱）條例》（香港法例第486章）處理用戶的個人資料，詳情請參閱我們的隱私政策。',
      ]
    },
    {
      title: '7. 條款修改',
      clauses: [
        '7.1 本公司保留隨時修改本條款的權利，修改後的條款將於APP內公布，用戶繼續使用服務即視為接受更新後的條款。',
      ]
    },
    {
      title: '8. 適用法律及管轄權',
      clauses: [
        '8.1 本條款受香港特別行政區法律管轄，並按香港法律解釋。',
        '8.2 任何因本條款引起的爭議，應提交香港法院專屬管轄。',
      ]
    },
    {
      title: '最後更新日期：2025年09月25日',
      clauses: []
    }
  ];
  const sectionsEn = [
    {
      clauses: [
        'Welcome to the electric vehicle charging service (hereinafter referred to as “the Service”) provided by Crazy Charge (hereinafter referred to as “the Company” or “We”). Please readthe following terms and conditions (hereinafter referred to as "these Terms") carefully. By using the Crazy Charge application, you agree to abide by all the contents of these Terms.'
      ]
    },
    {
    title: '1. Service Usage',
    clauses: [
      '1.1 Users must recharge, make payments, and initiate charging services through the Company\'s APP.',
      '1.2 Users must ensure the information provided (including license plate number, payment details) is true, accurate, and complete, and are responsible for all activities under their account.',
      '1.3 The Company reserves the right to modify, suspend, or terminate the Service at any time without prior notice to users.'
    ]
  },
  {
    title: '2. Recharge and Payment',
    clauses: [
      '2.1 Users can recharge their account through the APP. The recharged amount will be deposited into the user\'s account.',
      '2.2 Recharged amounts have no expiry date and can be retained by the user for future use.',
      '2.3 The Company may offer promotions (such as discounts, bonus credits, etc.) from time to time. Relevant promotions are subject to specific terms; please refer to the promotion descriptions within the APP for details.'
    ]
  },
  {
    title: '3. Refund Policy',
    clauses: [
      '3.1 Users must submit refund requests via WhatsApp, which will be reviewed by the Company.',
      '3.2 The refund amount will be deducted of all bonuses involved in that particular recharge (if applicable).',
      '3.3 An administrative fee of HKD 150 will be charged for each refund transaction and deducted from the refund amount.',
      '3.4 Considering administrative costs and resources, users are advised to retain unused recharge balances for future urgent charging needs rather than applying for a refund.',
      '3.5 The refund processing time is determined by the Company and may be affected by administrative, banking, or payment platform factors.'
    ]
  },
  {
    title: '4. Limitation of Liability',
    clauses: [
      '4.1 The Company will endeavor to ensure the stability and accuracy of the Service but does not guarantee that the Service will be uninterrupted, timely, secure, or error-free.',
      '4.2 Users understand and agree that the Company shall not be liable for any direct or indirect losses (including but not limited to data loss, time loss, or profit loss) incurred due to the use of the Service.',
      '4.3 The Company shall not bear any responsibility if service interruption or data loss is caused by force majeure events (such as natural disasters, network failures, government actions, etc.).'
    ]
  },
  {
    title: '5. User Conduct and Responsibilities',
    clauses: [
      '5.1 Users must retrieve their vehicle and leave the car park within 15 minutes after their vehicle\'s charging is completed. Failure to do so will result in an illegal parking fine of HK$3 per minute.',
      '5.2 If the user\'s vehicle abuses the Company\'s free parking time policy, including but not limited to parking within our charging station area without charging, or continuing to park after charging is complete, the Company reserves the right to take further action, including but not limited to wheel clamping (unlocking fee HK$2,000) and timing-based fines (HK$3/minute).',
      `5.3 Users shall not engage in any activities that violate Hong Kong laws or affect the normal operation of the Service, including but not limited to:
- Unauthorized access to others' accounts;
- Exploiting system vulnerabilities or engaging in any form of fraudulent activity;
- Interfering with, damaging, or overloading the Company's servers or networks.`,
      '5.4 If any loss or damage arises from the user\'s improper or unreasonable use of the Service, or hindering other users\' normal use of the Service, the user must bear all resulting claims, demands, lawsuits, and other legal liabilities.',
      '5.5 If customer\'s vehicle is parked or driven into or out of the Company\'s premises without actual usage of the charging service OR left a charged car after the charging has been completed, it will be considered as illegal parking subject to penalty or could be towed away. The Company is not responsible for any loss &/or damage sustained directly or indirectly by the vehicle. This Company will not be answerable to any car park liability.',
      '5.6 The Company shall not be responsible for any accidents or losses occurring within the charging station area involving users who violate these Terms.',
      '5.7 The Company reserves the right to pursue compensation for losses incurred by the Company, Service users, or any third parties due to user conduct.',
      '5.8 If a user violates these Terms, the Company has the right to suspend or terminate their account and reserves the right to pursue legal liability.'
    ]
  },
  {
    title: '6. Privacy Policy',
    clauses: [
      '6.1 The Company will handle users\' personal data in accordance with the Personal Data (Privacy) Ordinance (Chapter 486 of the Laws of Hong Kong). For details, please refer to our Privacy Policy.'
    ]
  },
  {
    title: '7. Modification of Terms',
    clauses: [
      '7.1 The Company reserves the right to modify these Terms at any time. The modified Terms will be announced within the APP. Continued use of the Service constitutes acceptance of the updated Terms.'
    ]
  },
  {
    title: '8. Governing Law and Jurisdiction',
    clauses: [
      '8.1 These Terms are governed by and shall be construed in accordance with the laws of the Hong Kong Special Administrative Region.',
      '8.2 Any disputes arising from these Terms and conditions shall be submitted to the exclusive jurisdiction of the Hong Kong courts.'
    ]
  },
  {
    title: 'Last Updated: 25 September 2025',
    clauses: []
  }

  ]
  const handleAgree = () => {
    router.back()
    // onAgree();
  };

  return (
    <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.scrollContainer}>
        {curren?.value?
        <View>
          <Text style={styles.header}>Crazy Charge 用戶條款及細則</Text>
            <View style={styles.section}>
              {sectionsZh.map((section, index) => (
                <TermsSection 
                  key={index}
                  title={section.title}
                  clauses={section.clauses}
                />
              ))}
            </View>
          </View>
          :
          <View>
            <Text style={styles.header}>Crazy Charge Users’ Terms and Conditions</Text>
            <View style={styles.section}>
              {sectionsEn.map((section, index) => (
                <TermsSection 
                  key={index}
                  title={section.title}
                  clauses={section.clauses}
                />
              ))}
            </View>
          </View>
        }

      </ScrollView>
      <NormalButton
        extendedStyle={styles.agreeButton}
        onPress={() => handleAgree()}
        title={<Text
            style={{
                fontWeight: '600',
                fontSize: 18,
                color: '#fff'
            }}
        >
          {t("common.agree")}
        </Text>}
        >

        </NormalButton>
    </View>
    </SafeAreaView>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 8,
    backgroundColor: '#f8f9fa'
  },
  scrollContainer: {
    paddingBottom: 70
  },
  header: {
    fontSize: 24,
    fontWeight: 'bold',
    marginBottom: 15,
    color: '#2c3e50',
    textAlign: 'center'
  },
  description: {
    fontSize: 14, 
    color: '#2c3e50',
    paddingLeft: 20,
    paddingRight: 20,
    marginBottom: 10,
  },
  section: {
    marginBottom: 10,
    backgroundColor: 'white',
    borderRadius: 8,
    padding: 16,
    elevation: 2
  },
  sectionTitle: {
    fontSize: 18,
    fontWeight: '600',
    marginBottom: 12,
    color: '#025c72'
  },
  clause: {
    fontSize: 14,
    lineHeight: 20,
    marginBottom: 5,
    color: '#34495e',
    flexWrap: 'wrap',      // 允许文本换行
    textAlign: 'justify'
  },
  agreeButton: {
    position: 'absolute',
    bottom: 10,
    left: 20,
    right: 20,
    padding: 16,
    alignItems: 'center'
  },
});

export default UserTermsPageComponent;
