import 'package:chat/models/ChatMsg.dart';
import 'package:chat/proto/chat.pb.dart';
import 'package:chat/r.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';

class FileMsgItem extends StatelessWidget {
  final MsgModel msgModel;
  FileMsgItem(this.msgModel);

  getTypeImage(String type) {
    type = type.toLowerCase();
    switch (type) {
      case 'pdf':
        return R.assetsImagesExtPdf;
        break;
      case 'doc':
      case 'docx':
        return R.assetsImagesExtWord;
        break;
      case 'ppt':
        return R.assetsImagesExtPpt;
        break;
      case 'xls':
      case 'xlsx':
        return R.assetsImagesExtExcel;
        break;
      case 'txt':
      case 'xml':
      case 'html':
      case 'htm':
      case '.h':
      case '.cpp':
      case '.conf':
      case '.c':
        return R.assetsImagesExtTxt;
        break;
      case 'tar':
      case 'gz':
      case 'gzip':
      case 'tgz':
      case 'zip':
        return R.assetsImagesExtZip;
        break;
      case 'mp3':
      case 'wav':
        return R.assetsImagesExtSound;
        break;

      case 'mp4':
      case 'avi':
      case 'mpg':
      case 'wm':
      case 'wmv':
        return R.assetsImagesExtVideo;
        break;
      case 'apk':
        return R.assetsImagesExtApk;
        break;
      default:
        return R.assetsImagesExtUnknown;
    }
  }

  @override
  Widget build(BuildContext context) {
    var fileMsg = FileChat.fromBuffer(msgModel.msgContent);
    var type = fileMsg.type;
    var size = fileMsg.size;
    var name = fileMsg.name;

    var sizeStr = '${(size / 1024).toStringAsFixed(1)}K';
    if (size > 1024 * 1024) {
      sizeStr = '${(size / 1024 / 1024).toStringAsFixed(1)}M';
    }
    return InkWell(
      onTap: msgModel.localFile != null
          ? () {
              print('打开文件 ${msgModel.localFile}');
              OpenFile.open(msgModel.localFile);
            }
          : null,
      child: Container(
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Image.asset(getTypeImage(type), width: 45),
            Padding(
                padding: EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      child: fixedText(name, fontSize: 13),
                      constraints: BoxConstraints(
                          maxWidth: Screen.width - 120 - 122, maxHeight: 50),
                    ),
                    Expanded(child: SizedBox()),
                    fixedText(sizeStr, fontSize: 12, color: Colors.grey)
                  ],
                )),
            msgModel.localFile == null
                ? Container(
                    constraints: BoxConstraints(
                      maxWidth: 40,
                    ),
                    child: fixedText('未下载', fontSize: 12, color: Colors.grey))
                : Container()
          ],
        ),
      ),
    );
  }
}
