import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:wanna_battle/model/PlayerInfo.dart';
import '../model/HouseInfo.dart';
import '../model/GameInfo.dart';
import '../pages/RoomInfo.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'dart:ui';

typedef void OnTapHomeMenu();

class HouseItem extends StatelessWidget {
  HouseItem(this.houseInfo, this.gameInfo, {this.playerInfo = null, this.isNext = true, this.onTapHomeMenu, this.onBack, this.onNext, this.isHome = false});
  HouseInfo houseInfo;
  GameInfo gameInfo;
  PlayerInfo playerInfo;
  bool isNext;
  OnTapHomeMenu onTapHomeMenu;
  OnTapHomeMenu onBack;
  OnTapHomeMenu onNext;
  bool isHome;

  List<String> imageList = ['images/img_fangjian_hong.png', 'images/img_fangjian_huise.png'];

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.only(bottom: 10, left: 15, right: 15),
        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(4))),
        child: Material(
            color: Colors.transparent,
            child: InkWell(
                onTap: () async {
                  if (!isNext) {
                    onTapHomeMenu();
                  } else {
                    if (isHome) {
                      onNext();
                    }

                    bool res = await Navigator.push(context, CupertinoPageRoute(builder: (context) => RoomInfo(roomId: houseInfo.id.toString())));
                    if (res != null && res) {
                      if (isHome) {
                        onBack();
                      }
                    }
                  }
                  //
                },
                child: Container(
                  height: 80,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage(imageList[houseInfo.statusFlag == 0 ? 0 : 1]), fit: BoxFit.fitHeight, alignment: Alignment.bottomRight),
                  ),
                  padding: EdgeInsets.all(10),
                  child: Row(
                    children: <Widget>[
                      ClipRRect(
                        borderRadius: BorderRadius.circular(4.0),
                        child: CachedNetworkImage(
                          imageUrl: gameInfo.icon,
                          width: 60,
                          height: 60,
                          fit: BoxFit.cover,
                        ),
                      ),
                      Container(
                        width: 10,
                      ),
                      Expanded(
                        flex: 1,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text(houseInfo.houseName, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 16, color: Colors.black)),
                            Container(
                              height: 4,
                            ),
                            Text('目标:杀戮${houseInfo.killnumber}人', style: TextStyle(fontSize: 13, color: Color(0xFF999999)))
                          ],
                        ),
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          _topWidget(playerInfo),
                          Row(
                            children: <Widget>[
                              Image.asset('images/house1.png'),
                              Text(
                                '${houseInfo.playerNumber != null ? houseInfo.playerNumber : 0}/${houseInfo.maxNumber}',
                                style: TextStyle(color: Colors.white, fontSize: 14),
                              )
                            ],
                          )
                        ],
                      )
                    ],
                  ),
                ))));
  }

  Widget _topWidget(PlayerInfo playerInfo) {
    if (playerInfo == null) {
      if (houseInfo.statusFlag == 0) {
        return Image.asset('images/join.png');
      } else if (houseInfo.statusFlag == 2) {
        return Text('进行中', style: TextStyle(color: Color(0xFF5C5C5C), fontSize: 14, fontWeight: FontWeight.w600));
      } else if (houseInfo.statusFlag == 3) {
        return Text('解析中', style: TextStyle(color: Color(0xFF5C5C5C), fontSize: 14, fontWeight: FontWeight.w600));
      } else if (houseInfo.statusFlag == 8) {
        return Text('结算中', style: TextStyle(color: Color(0xFF5C5C5C), fontSize: 14, fontWeight: FontWeight.w600));
      } else {
        return Text('已结束', style: TextStyle(color: Color(0xFF5C5C5C), fontSize: 14, fontWeight: FontWeight.w600));
      }
    } else {
      if (houseInfo.statusFlag == 2 || houseInfo.statusFlag == 3) {
        return Text('待结算', style: TextStyle(color: Color(0xFF5C5C5C), fontSize: 14, fontWeight: FontWeight.w600));
      } else if (houseInfo.statusFlag == 8) {
        return Text('结算中', style: TextStyle(color: Color(0xFF5C5C5C), fontSize: 14, fontWeight: FontWeight.w600));
      } else if (playerInfo.houseRank != null &&
          playerInfo.statusFlag != 7 &&
          playerInfo.statusFlag != 9 &&
          !playerInfo.dataError &&
          playerInfo.statusFlag != 6 &&
          playerInfo.killNumber >= playerInfo.needkill) {
        if (playerInfo.houseRank < 4) {
          return Text('第${playerInfo.houseRank}名', style: TextStyle(color: Color(0xFFD4504B), fontSize: 14, fontWeight: FontWeight.w600));
        } else {
          return Text('第${playerInfo.houseRank}名', style: TextStyle(color: Color(0xFF5C5C5C), fontSize: 14, fontWeight: FontWeight.w600));
        }
      } else {
        return Text('任务失败', style: TextStyle(color: Color(0xFF5C5C5C), fontSize: 14, fontWeight: FontWeight.w600));
      }
    }
    // return Container();
  }
}
