import 'package:flutter/material.dart';
import 'package:flutter_wallet/base/YSBase.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/tools/YSNetWork.dart';
import 'package:flutter_wallet/tools/YSTools.dart';

import '../tools/YSColors.dart';

class YSTansDetail extends StatefulWidget {
  final Map item;
  final String tokenAddress;
  const YSTansDetail({Key? key, required this.item, required this.tokenAddress}) : super(key: key);

  @override
  YSTansDetailState createState() => YSTansDetailState();
}

class YSTansDetailState extends State<YSTansDetail> {
  final List _array1 = [];
  final List _array2 = [];
  String _hash = '';
  String _status = '2';
  @override
  void initState() {
    _getArrayData(widget.item);
    super.initState();
  }
  _getArrayData(Map item) async{
    _array1.clear();
    _array2.clear();
    _status = '${item['status']}';
    _array1.addAll([
      {'title':S.current.FASONGFANG,'value':item['from_addr'],'isCopy':true},
      {'title':S.current.JIESHOUFANG,'value':item['to_addr'],'isCopy':true},
      if(item['gas_price']!=null){'title':S.current.WANGLUOFEI,'value':toDecimalFlex(double.parse('${item['gas_price']}'),decimal: 18,range: 10),'isCopy':false},
    ]);
    _array2.addAll([
      {'title':S.current.JIAOYIHAXI,'value':item['hash'],'isCopy':true},
      if(item['block_number']!=null){'title':S.current.QUKUAIGAODU,'value':'${item['block_number']??''}','isCopy':true},
      if(item['timestamp']!=null){'title':S.current.JIAOYISHIJIAN,'value':toDateFlex(item['timestamp']),'isCopy':false},
    ]);
    _hash = widget.item['hash'];
    setState(() {});
  }
  Future<void> _refresh() async{
    Map request = {'chain_id':YSData().typeId,'address':YSData().wallet['public'],'type':0,'page':1,'count':100};
    if(YSData().wallet['public']!=widget.tokenAddress){
      request['contract_address'] = widget.tokenAddress;
    }
    YSNetWork.ysRequestHttp(context, type: RequestType.get, api: 'transactions', parameter: request, successSetter: (dict){
      List list = dict['data']['list']??[];
      List array = list.where((element) => element['hash']==widget.item['hash']).toList();
      if(array.isNotEmpty){
        Map item = array.first;
        _getArrayData(item);
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ysTitle: S.current.JIAOYIXIANGQING,
      ysChild: Container(
        width: ysWidth(context),
        height: ysHeight(context)-ysTOP(context)-60,
        margin: EdgeInsets.only(left: hsp(20),right: hsp(20)),
        padding: EdgeInsets.only(top: hsp(10)),
        child: RefreshIndicator(
          onRefresh: _refresh,
          child: SingleChildScrollView(
            physics: const AlwaysScrollableScrollPhysics(),
            child: Column(
              children: [
                //${'${item['status']}'=='1'?'交易成功':'${item['status']}'=='0'?'交易失败':'交易中'}
                _status=='1'?Icon(Icons.check_circle,size: hsp(60),color: Colors.green,):
                _status=='0'?Icon(Icons.error,size: hsp(60),color: Colors.red,):Icon(Icons.watch_later_rounded,size: hsp(60),color: Colors.grey,),
                Padding(
                  padding: EdgeInsets.only(top: hsp(5),bottom: hsp(5)),
                  child: Text(_status=='1'?S.current.JIAOYICHENGGONG:_status=='0'?S.current.JIAOYISHIBAI:S.current.JIAOYIZHONG,style: TextStyle(fontSize: zsp(14),color: _status=='1'?Colors.green:_status=='0'?Colors.red:Colors.grey),)
                ),
                Text('${widget.item['type']==2?'-':widget.item['type']==1?'+':''}${toDecimalFlex((widget.item['value']??
                    double.parse(widget.item['amount'])??0),decimal: widget.item['decimal']??0,range: 5)}${widget.item['symbol']??''}',
                  style: TextStyle(fontSize: zsp(16),color: widget.item['type']==1?const Color(0xFF68AF64):const Color(0xFFDE5656)),),
                Container(
                  margin: EdgeInsets.only(top: hsp(20)),
                  decoration: BoxDecoration(
                    borderRadius: const BorderRadius.all(Radius.circular(10)),
                    color: YSColors.containColor(context),
                    boxShadow: [
                      BoxShadow(color: YSColors.lineColor(context),blurRadius: 3)
                    ],
                  ),
                  padding: EdgeInsets.all(hsp(20)),
                  child: ListView.builder(
                    itemBuilder: (context,index){
                      Map item = _array1[index];
                      return Padding(
                        padding: EdgeInsets.only(top: hsp(10)),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(item['title'],style: YSColors.subStyle(context),),
                            Row(
                              children: [
                                Text(ysTextCut(item['value'],number: 8),style: YSColors.contentStyle(context),),
                                if(item['isCopy']==true)Padding(
                                  padding: EdgeInsets.only(left: hsp(5)),
                                  child: GestureDetector(
                                    onTap: (){
                                      YSCopyPaste.copy(item['value']);
                                    },
                                    behavior: HitTestBehavior.opaque,
                                    child: Image.asset(YSColors.imageStyle(context, '复制'),height: hsp(15),width: hsp(15),),
                                  ),
                                )
                              ],
                            )
                          ],
                        ),
                      );
                    },
                    padding: const EdgeInsets.all(0),
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: _array1.length,
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: hsp(20)),
                  decoration: BoxDecoration(
                    borderRadius: const BorderRadius.all(Radius.circular(10)),
                    color: YSColors.containColor(context),
                    boxShadow: [
                      BoxShadow(color: YSColors.lineColor(context),blurRadius: 3)
                    ],
                  ),
                  padding: EdgeInsets.all(hsp(20)),
                  child: Row(
                    children: [
                      Expanded(child: ListView.builder(
                        itemBuilder: (context,index){
                          Map item = _array2[index];
                          return Padding(
                            padding: EdgeInsets.only(top: hsp(10)),
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(item['title'],style: YSColors.subStyle(context),),
                                Row(
                                  children: [
                                    Text(ysTextCut(item['value'],number: 8),style: YSColors.contentStyle(context),),
                                    if(item['isCopy']==true)Padding(
                                      padding: EdgeInsets.only(left: hsp(5)),
                                      child: GestureDetector(
                                        onTap: (){
                                          YSCopyPaste.copy(item['value']);
                                        },
                                        behavior: HitTestBehavior.opaque,
                                        child: Image.asset(YSColors.imageStyle(context, '复制'),height: hsp(15),width: hsp(15),),
                                      ),
                                    )
                                  ],
                                )
                              ],
                            ),
                          );
                        },
                        padding: const EdgeInsets.all(0),
                        physics: const NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: _array2.length,
                      )),
                      SizedBox(
                        height: hsp(100),
                        width: hsp(100),
                        child: YSCodeImage.codeImage(_hash),
                      )
                    ],
                  ),
                )

              ],
            ),
          ),
        ),
      ),
    );
  }
}

class YSTansDetail2 extends StatefulWidget {
  final Map item;
  const YSTansDetail2({Key? key, required this.item}) : super(key: key);

  @override
  YSTansDetail2State createState() => YSTansDetail2State();
}

class YSTansDetail2State extends State<YSTansDetail2> {
  final List _array1 = [];
  final List _array2 = [];
  String _hash = '';
  int _status = 2;

  _getArrayData(Map item) async{
    _array1.clear();
    _array2.clear();
    _status = item['TxStatus'];
    _array1.addAll([
      {'title':S.current.FASONGFANG,'value':item['FromAddress'],'isCopy':true},
      {'title':S.current.JIESHOUFANG,'value':item['ToAddress'],'isCopy':true},
      if(item['gas_price']!=null){'title':S.current.WANGLUOFEI,'value':toDecimalFlex(item['gas_price'],decimal: item['Decimal'],range: 6),'isCopy':false},
    ]);
    _array2.addAll([
      {'title':S.current.JIAOYIHAXI,'value':item['Hash'],'isCopy':true},
      if(item['BlockNum']!=null){'title':S.current.QUKUAIGAODU,'value':'${item['BlockNum']??''}','isCopy':true},
      if(item['CreateTime']!=null){'title':S.current.JIAOYISHIJIAN,'value':item['CreateTime'],'isCopy':false},
    ]);
    _hash = widget.item['Hash'];
    setState(() {});
  }
  @override
  void initState() {
    _getArrayData(widget.item);
    _postRead();
    super.initState();
  }
  _postRead() async{
    Map request = {};
    request['ids'] = widget.item['ID'];
    request['address'] = YSData().wallet['public'];
    request['chain_id'] = YSData().typeId;
    Map dict = await YSNetWork.ysRequestHttpNOSet(context, type: RequestType.get, api: 'settransnotice', parameter: request);
    if(dict.isNotEmpty){

    }
  }
  @override
  Widget build(BuildContext context) {
    LogUtil.d(widget.item);
    return YSBase(
      ysTitle: S.current.JIAOYIXIANGQING,
      ysChild: Container(
        width: ysWidth(context),
        height: ysHeight(context)-ysTOP(context)-60,
        margin: EdgeInsets.only(left: hsp(20),right: hsp(20)),
        padding: EdgeInsets.only(top: hsp(10)),
        child: SingleChildScrollView(
          physics: const AlwaysScrollableScrollPhysics(),
          child: Column(
            children: [
              //${'${item['status']}'=='1'?'交易成功':'${item['status']}'=='0'?'交易失败':'交易中'}
              _status==1?Icon(Icons.check_circle,size: hsp(60),color: Colors.green,):Icon(Icons.error,size: hsp(60),color: Colors.red,),
              Padding(
                  padding: EdgeInsets.only(top: hsp(5),bottom: hsp(5)),
                  child: Text(_status==1?S.current.JIAOYICHENGGONG:_status==0?S.current.JIAOYISHIBAI:'',style: TextStyle(fontSize: zsp(14),color: _status==1?Colors.green:Colors.red),)
              ),
              Text('${widget.item['type']==2?'-':widget.item['type']==1?'+':''}${toDecimalFlex(widget.item['Amount'],decimal: widget.item['Decimal']??0,range: 6)}${widget.item['Symbol']??''}',
                style: TextStyle(fontSize: zsp(16),color: widget.item['type']==1?const Color(0xFF68AF64):const Color(0xFFDE5656)),),
              Container(
                margin: EdgeInsets.only(top: hsp(20)),
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.all(Radius.circular(10)),
                  color: YSColors.containColor(context),
                  boxShadow: [
                    BoxShadow(color: YSColors.lineColor(context),blurRadius: 3)
                  ],
                ),
                padding: EdgeInsets.all(hsp(20)),
                child: ListView.builder(
                  itemBuilder: (context,index){
                    Map item = _array1[index];
                    return Padding(
                      padding: EdgeInsets.only(top: hsp(10)),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(item['title'],style: YSColors.subStyle(context),),
                          Row(
                            children: [
                              Text(ysTextCut(item['value'],number: 8),style: YSColors.contentStyle(context),),
                              if(item['isCopy']==true)Padding(
                                padding: EdgeInsets.only(left: hsp(5)),
                                child: GestureDetector(
                                  onTap: (){
                                    YSCopyPaste.copy(item['value']);
                                  },
                                  behavior: HitTestBehavior.opaque,
                                  child: Image.asset(YSColors.imageStyle(context, '复制'),height: hsp(15),width: hsp(15),),
                                ),
                              )
                            ],
                          )
                        ],
                      ),
                    );
                  },
                  padding: const EdgeInsets.all(0),
                  physics: const NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: _array1.length,
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: hsp(20)),
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.all(Radius.circular(10)),
                  color: YSColors.containColor(context),
                  boxShadow: [
                    BoxShadow(color: YSColors.lineColor(context),blurRadius: 3)
                  ],
                ),
                padding: EdgeInsets.all(hsp(20)),
                child: Row(
                  children: [
                    Expanded(child: ListView.builder(
                      itemBuilder: (context,index){
                        Map item = _array2[index];
                        return Padding(
                          padding: EdgeInsets.only(top: hsp(10)),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(item['title'],style: YSColors.subStyle(context),),
                              Row(
                                children: [
                                  Text(ysTextCut(item['value'],number: 8),style: YSColors.contentStyle(context),),
                                  if(item['isCopy']==true)Padding(
                                    padding: EdgeInsets.only(left: hsp(5)),
                                    child: GestureDetector(
                                      onTap: (){
                                        YSCopyPaste.copy(item['value']);
                                      },
                                      behavior: HitTestBehavior.opaque,
                                      child: Image.asset(YSColors.imageStyle(context, '复制'),height: hsp(15),width: hsp(15),),
                                    ),
                                  )
                                ],
                              )
                            ],
                          ),
                        );
                      },
                      padding: const EdgeInsets.all(0),
                      physics: const NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      itemCount: _array2.length,
                    )),
                    SizedBox(
                      height: hsp(100),
                      width: hsp(100),
                      child: YSCodeImage.codeImage(_hash),
                    )
                  ],
                ),
              )

            ],
          ),
        ),
      ),
    );
  }
}

