import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../index.dart';

class IdCardInfo extends GetView<IdCardScanController> {
  const IdCardInfo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => AnimatedContainer(
        duration: const Duration(milliseconds: 300),
        width: controller.state.isIdCardInfoShow ? 300 : 0,
        color: const Color.fromARGB(255, 231, 231, 231),
        child: OverflowBox(
          maxWidth: 300,
          minWidth: 300,
          alignment: Alignment.centerRight,
          child: Column(
            children: [
              const SizedBox(height: 20),
              Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                ),
                clipBehavior: Clip.antiAlias,
                child: Transform.scale(
                  scale:
                      controller.idCardInfo.localCardImagePath == '' ? 1 : 1.8,
                  child: Image.file(
                    File(controller.idCardInfo.localCardImagePath),
                    errorBuilder: (context, error, stackTrace) {
                      return const Image(
                        image: AssetImage('assets/images/id_card.png'),
                        color: Colors.grey,
                        width: 220,
                      );
                    },
                    width: 220,
                  ),
                ),
              ),
              const SizedBox(height: 20),
              Expanded(
                child: Scrollbar(
                  thumbVisibility: true,
                  thickness: 10,
                  radius: const Radius.circular(10),
                  child: ListView(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    children: [
                      _buildLabel('姓名'),
                      _buildValue(controller.idCardInfo.idCardName),
                      const SizedBox(height: 10),
                      _buildLabel('性别'),
                      _buildValue(controller.idCardInfo.idCardGender),
                      const SizedBox(height: 10),
                      _buildLabel('民族'),
                      _buildValue(controller.idCardInfo.idCardNation),
                      const SizedBox(height: 10),
                      _buildLabel('出生'),
                      _buildValue(controller.idCardInfo.idCardBirthDate),
                      const SizedBox(height: 10),
                      _buildLabel('住址'),
                      _buildValue(controller.idCardInfo.idCardAddress),
                      const SizedBox(height: 10),
                      _buildLabel('公民身份号码'),
                      _buildValue(controller.idCardInfo.idCardNumber),
                      const SizedBox(height: 10),
                    ],
                  ),
                ),
              ),
              _buildCaptureAgainButton(),
              const SizedBox(height: 10),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildLabel(String label) {
    return Row(
      children: [
        Padding(
          padding: const EdgeInsets.only(left: 20),
          child: Text(
            label,
            style: const TextStyle(color: Colors.grey, fontSize: 18),
          ),
        ),
        Expanded(child: Container()),
        // TODO 预留手动编辑入口
        // InkWell(
        //   onTap: () {},
        //   child: Container(
        //     padding:
        //         const EdgeInsets.only(left: 20, top: 2, bottom: 2, right: 20),
        //     child: const Icon(
        //       Icons.border_color_outlined,
        //       color: Colors.grey,
        //       size: 20,
        //     ),
        //   ),
        // ),
      ],
    );
  }

  Widget _buildValue(String value) {
    return Align(
      alignment: Alignment.centerLeft,
      child: Padding(
        padding: const EdgeInsets.only(left: 20),
        child: Text(
          value,
          style: const TextStyle(color: Colors.black87, fontSize: 20),
        ),
      ),
    );
  }

  Widget _buildCaptureAgainButton() {
    return Align(
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Text(
            '身份证信息不正确？',
            style: TextStyle(color: Colors.grey),
          ),
          TextButton(
            onPressed: () {
              controller.state.isIdCardInfoShow = false;
              controller.state.isShowIdCardInfoSwitch = false;
              controller.state.isInIdCardScan = false;
              controller.openBackCamera();
            },
            child: const Text(
              '重新拍摄',
              style: TextStyle(color: Colors.blue),
            ),
          ),
        ],
      ),
    );
  }
}
