package com.tofly.ghrq_route.common.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;

import com.gaoyuan.weixinrecord.example.Record;
import com.gaoyuan.weixinrecord.manager.MediaManager;
import com.mgq.base.utils.progressdialog.ProgressManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.module.message.controller.MessageCallBack;
import com.tofly.ghrq_route.module.message.controller.MessageController;
import com.tofly.ghrq_route.module.message.domain.InMessageBean;
import com.tofly.ghrq_route.module.message.domain.InMessageBean.FilesBean;
import com.tofly.ghrq_route.module.message.domain.MessageDeatilBean;
import com.tofly.ghrq_route.module.message.domain.OutMessageBean;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.trouble.controller.TroubleCallBack;
import com.tofly.ghrq_route.module.trouble.controller.TroubleController;
import com.tofly.ghrq_route.module.trouble.domain.TroubleAudioBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleList;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.image.ImageUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/5/9.
 */

public class UiMessageDetail extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.text_user_name)
    TextView textUserName;
    @BindView(R.id.text_send_date)
    TextView textSendDate;
    @BindView(R.id.layout_addition_area)
    LinearLayout layoutAdditionArea;
    @BindView(R.id.text_addition_num)
    TextView textAdditionNum;
    @BindView(R.id.layout_message_addition)
    LinearLayout layoutMessageAddition;
    @BindView(R.id.text_message_content)
    TextView textMessageContent;
    @BindView(R.id.layout_message_replay)
    LinearLayout layoutMessageReplay;
    @BindView(R.id.tv_replay)
    TextView tvReplay;
    @BindView(R.id.v_view)
    View vView;

    private DBDaoHelper dbDaoHelper;

    private UserBean mUser;
    private Boolean mIsOutMessage = false;
    private InMessageBean mMessageBean;
    private OutMessageBean mOutMessageBean;

    private MessageController messageController;

    private TroubleController troubleController;

    private ArrayList<String> mAdditionPhotos;

    private List<Record> mAudioRecords;
    private List<AnimationDrawable> mAnimationDrawables;
    private List<FilesBean> filesBeans;

    private String Id;
    private int createUserId;
    private String name, time, type, module;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_message_detail);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mUser = BaseApp.getInstance().getLoginUser();
        dbDaoHelper = DBDaoHelper.getInstance();
        Id = getIntent().getStringExtra("messageId");
        name = getIntent().getStringExtra("name");
        time = getIntent().getStringExtra("time");
        createUserId = getIntent().getIntExtra("createUserId", -1);
        type = getIntent().getStringExtra("type");
        module = getIntent().getStringExtra(InMessageBean.MESSAGE_MODULE_KEY);
        initData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_message_detail_reply, menu);
        MenuItem itemReply = menu.findItem(R.id.action_reply);
        if (!TextUtils.isEmpty(type)) {
            if (type.equals("0")) {
                if (mIsOutMessage) {
                    itemReply.setVisible(false);
                } else {
                    itemReply.setVisible(true);
                }
            } else {
                itemReply.setVisible(false);
                menu.findItem(R.id.action_goto).setVisible(true);
            }
        }
        /*不能给管理员回复信息*/
/*            if (mMessageBean != null){
                String senderId = mMessageBean.getSend_userid();
                if (senderId.equals(InMessageBean.ADMIN_USER_ID)){
                    itemReply.setVisible(false);
                }
            }*/
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_reply: {
                Intent intent = new Intent();
                intent.setClass(UiMessageDetail.this, UiMessageReply.class);
                intent.putExtra("id", Id);
                intent.putExtra("createUserId", createUserId);
                startActivityForResult(intent, 0x002);
            }
            break;
            case R.id.action_delete: {
                showRemoveAlert();
            }
            case R.id.action_goto:
                if (!TextUtils.isEmpty(type)) {
                    if (type.equals("1")) {
                        intentActivity(UiRepair.class);
                    } else if (type.equals("2")) {
                        intentActivity(UiPlan.class);
                    } else if (type.equals("3")) {
                        intentActivity(UiBuildManage.class);
                    } else if (type.equals("4")) {
                        intentActivity(UiTroubleList.class);
                    } else if (type.equals("5")) {
                        intentActivity(UiJobList.class);
                    }
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void intentActivity(Class c) {
        Intent intent = new Intent(UiMessageDetail.this, c);
        if (module != null && !module.isEmpty()) {
            Bundle bundle = new Bundle();
            bundle.putString(InMessageBean.MESSAGE_MODULE_KEY, module);
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    private void showRemoveAlert() {
        String title = "删除信息";
        String content = "是否删除信息?";
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(content);
        builder.setPositiveButton(android.R.string.ok, (dialog, which) -> removeMessage());
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.show();
    }

    private void removeMessage() {
        if (mIsOutMessage) {
            if (mOutMessageBean != null) {
                List<String> messageIds = new ArrayList<String>();
                messageIds.add(mOutMessageBean.getRowId());
                deleteSendMessage(messageIds);
            }
        } else {
            if (mMessageBean != null) {
                List<String> messageIds = new ArrayList<String>();
                messageIds.add(String.valueOf(mMessageBean.getMessageId()));
                deleteRecvMessage(messageIds);
            }
        }
    }

    private void initData() {
        filesBeans = new ArrayList<>();
        mAnimationDrawables = new ArrayList<>();
        messageController = new MessageController(this);
        messageController.getMsgDetail(Id, new MessageCallBack() {
            @Override
            public void onDetailMsg(boolean isSuccess, int errorCoed, String errMs, MessageDeatilBean data) {
                super.onDetailMsg(isSuccess, errorCoed, errMs, data);
                if (isSuccess) {
                    if (data != null) {
                        /*发送者名称*/
                        if (!TextUtils.isEmpty(name))
                            textUserName.setText(name);
                        /*发送时间*/
                        if (!TextUtils.isEmpty(time))
                            textSendDate.setText(time);

                        if (!TextUtils.isEmpty(data.getMessage())) {
                            textMessageContent.setText(data.getMessage());
                        }
                        if (data.getPhotos() != null && data.getPhotos().size() > 0) {
                            for (String photo : data.getPhotos()) {
                                FilesBean filesBean = new FilesBean();
                                filesBean.setFiletype("image");
                                filesBean.setFilename(photo);
                                filesBeans.add(filesBean);
                            }
                        }
                        if (data.getAudios() != null && data.getAudios().size() > 0) {
                            for (String file : data.getAudios()) {
                                FilesBean filesBean = new FilesBean();
                                filesBean.setFiletype("audio");
                                filesBean.setFilename(file);
                                filesBeans.add(filesBean);
                            }

                        }
                        if (filesBeans.size() > 0) {
                            int totalNum = filesBeans.size();
                            for (FilesBean files : filesBeans) {
                                layoutAdditionArea.setVisibility(View.VISIBLE);
                                vView.setVisibility(View.VISIBLE);
                                View fileView = getViewFromFile(files, String.valueOf(data.getMessageId()));
                                layoutMessageAddition.addView(fileView);
                            }
                            textAdditionNum.setText(String.format("%d个附件", totalNum));
                        } else {
                            layoutAdditionArea.setVisibility(View.GONE);
                            vView.setVisibility(View.GONE);
                        }
                        //废弃的语音加载
//                        if (data.getAudios() != null && data.getAudios().size() > 0) {
//                            int totalSize = 0;
//                            int totalNum = data.getAudios().size();
//                            for (String file : data.getAudios()) {
//                                layoutAdditionArea.setVisibility(View.VISIBLE);
//                                if (file != null) {
//                                    View fileView = getViewFromFile(NetRequestURL.getHttpHead() + "?remote=" + file + "&access_token=" + mUser.getToken(), String.valueOf(Id), 1);
//                                       layoutMessageAddition.addView(fileView);
//                                    //  totalSize += file.getFilelength();
//                                }
//                            }
//                            textAdditionNum.setText(String.format("%d个附件", totalNum));
//                            textAddtionSize.setText(FileUtil.bytesToString(totalSize));
//                        } else {
//                            layoutAdditionArea.setVisibility(View.GONE);
//                        }
                        if (data.getReplyList() != null && data.getReplyList().size() > 0) {
                            tvReplay.setVisibility(View.VISIBLE);
                            for (MessageDeatilBean.MessageReplyBean messageReplyBean : data.getReplyList()) {
                                View view = getReplayView(messageReplyBean);
                                layoutMessageReplay.addView(view);
                            }
                        }
                    }
                } else {
                    toast(errMs);
                }
            }
        });
    }


    private View getViewFromFile(FilesBean filesBean, String messageId) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View rootView = inflater.inflate(R.layout.item_message_addition, null);

        final ImageView imageFileIcon = (ImageView) rootView.findViewById(R.id.image_file_icon);
        final LinearLayout audioLayout = (LinearLayout) rootView.findViewById(R.id.layout_audio_signal);
        TextView textFileName = (TextView) rootView.findViewById(R.id.text_file_name);
        //       TextView textFileSize = (TextView) rootView.findViewById(R.id.text_file_size);
        final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.progress_image);
        audioLayout.setVisibility(View.GONE);
        imageFileIcon.setVisibility(View.GONE);
        progressBar.setVisibility(View.INVISIBLE);
        String[] aar = filesBean.getFilename().split("/");
        if (aar.length > 0) {
            textFileName.setText(aar[aar.length - 1]);
        }
        //       String fileSize = FileUtil.bytesToString(filesBean.getFilelength());
//        if (fileSize != null) {
//            textFileSize.setText(fileSize);
//        }
        String sIconUrl = filesBean.getFilename();
//        String sFileType = filesBean.getFiletype();
        InMessageBean.File_Type file_type = filesBean.getFileType();
        if (sIconUrl != null) {
            String faceurl = NetRequestURL.newApi_dyrq.getFormattedPicture(sIconUrl);
            if (file_type.isEqual(InMessageBean.File_Type.FILE_TYPE_IMAGE)) {
                if (mAdditionPhotos == null)
                    mAdditionPhotos = new ArrayList<>();
                getPhotoView(faceurl, imageFileIcon, progressBar);
            } else if (file_type.isEqual(InMessageBean.File_Type.FILE_TYPE_AUDIO)) {
                if (mAudioRecords == null)
                    mAudioRecords = new ArrayList<Record>();
                int audioLenth = 1;
//                String strLenth = filesBean.getAudiolength();
//                if (strLenth != null) {
//                    try {
//                        audioLenth = Integer.parseInt(strLenth);
//                    } catch (NumberFormatException e) {
//                        audioLenth = 1;
//                    }
//                }
                /*strLenth = audioLenth <= 0 ? 1 + "''" : audioLenth + "''";
                if (fileSize != null){
                    fileSize += " (" + strLenth + ")";
                    textFileSize.setText(fileSize);
                }*/
                getAudioView(faceurl, audioLenth, messageId, audioLayout, progressBar);
            }
        }

        rootView.setTag(filesBean);

        return rootView;
    }

    private void getPhotoView(final String url, final ImageView imageFileIcon, final ProgressBar progressBar) {
        ImageLoader loader = ImageLoader.getInstance();
        DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                .showImageOnFail(R.mipmap.ico_user_name)
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中
                .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
                .imageScaleType(ImageScaleType.NONE)// 设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.ARGB_8888)// 设置图片的解码类型//
                .build();
        loader.displayImage(url, imageFileIcon, imageOptions, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {
                progressBar.setVisibility(View.VISIBLE);
                imageFileIcon.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {
                progressBar.setVisibility(View.GONE);
                imageFileIcon.setVisibility(View.VISIBLE);
                imageFileIcon.setImageResource(R.mipmap.ic_close_grey_400_24dp);
            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                progressBar.setVisibility(View.GONE);
                imageFileIcon.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });
        mAdditionPhotos.add(url);
        imageFileIcon.setTag(url);
        imageFileIcon.setOnClickListener(v -> {
            // 点击图片查看大图
            ImageView imageV = (ImageView) v;
            String PicAbsolutePath = imageV.getTag().toString();
            System.out.println(PicAbsolutePath);
            Bundle mBundle = new Bundle();
            mBundle.putString(ImageUtil.CURRENT_PATH_KEY, PicAbsolutePath);
            mBundle.putStringArrayList(ImageUtil.PICS_PATH_KEY, mAdditionPhotos);
            Intent intent = new Intent(UiMessageDetail.this, UiBigImageList.class);
            intent.putExtras(mBundle);
            startActivity(intent);
        });
    }

    private void resetData() {
        if (mAudioRecords != null) {
            for (Record record : mAudioRecords) {
                record.setPlaying(false);//保证在第二次点击该语音栏时当作没有“不是在播放状态”。
            }
        }
    }

    private void resetAnim(AnimationDrawable animationDrawable) {
        if (!mAnimationDrawables.contains(animationDrawable)) {
            mAnimationDrawables.add(animationDrawable);
        }
        for (AnimationDrawable ad : mAnimationDrawables) {
            ad.selectDrawable(0);
            ad.stop();
        }
    }

    private void getAudioView(final String url, final int audioLenth, final String messageId,
                              final LinearLayout audioLayout, final ProgressBar progressBar) {
        progressBar.setVisibility(View.VISIBLE);
        audioLayout.setVisibility(View.GONE);

        boolean bHasCache = false;
        TroubleAudioBean audioBean = dbDaoHelper.queryTroubleAudio(url);
        String filePath = null;
        if (audioBean != null) {
            filePath = audioBean.getPath();
            if (filePath != null && new File(filePath).exists()) {
                bHasCache = true;
            }
        }
        if (bHasCache) {
            Record record = new Record();
            record.setPath(filePath);
            record.setSecond(audioBean.getSecond());
            record.setPlayed(false);
            mAudioRecords.add(record);
            audioLayout.setTag(record);
            progressBar.setVisibility(View.GONE);
            audioLayout.setVisibility(View.VISIBLE);
        } else {
            UserBean user = BaseApp.getInstance().getLoginUser();
            if (troubleController == null)
                troubleController = new TroubleController(this);
            troubleController.getTroubleAudio(user.getToken(), url, new TroubleCallBack() {
                @Override
                public void onGetTroubleAudio(boolean isSuccess, int errorCode, String errMsg, String path) {
                    super.onGetTroubleAudio(isSuccess, errorCode, errMsg, path);
                    if (isSuccess) {
                        progressBar.setVisibility(View.GONE);
                        audioLayout.setVisibility(View.VISIBLE);

                        Record record = new Record();
                        record.setPath(path);
                        record.setSecond(audioLenth);
                        record.setPlayed(false);
                        mAudioRecords.add(record);
                        audioLayout.setTag(record);

                        TroubleAudioBean audioBean = new TroubleAudioBean();
                        String audioId = UUID.randomUUID().toString();
                        audioBean.setId(audioId);
                        audioBean.setTroubleid(messageId);
                        audioBean.setPath(path);
                        audioBean.setSecond(audioLenth);
                        audioBean.setUrl(url);
                        audioBean.setStatus(TroubleBean.TROUBLE_STATE);
                        dbDaoHelper.insertOrUpdateTroubleAudio(audioBean);
                    } else {
                        progressBar.setVisibility(View.GONE);
                        audioLayout.setVisibility(View.VISIBLE);
                        audioLayout.setBackgroundResource(R.mipmap.ic_close_grey_400_24dp);
                    }
                }
            });
        }

        audioLayout.setOnClickListener(view -> {
            Record record1 = (Record) view.getTag();
            if (record1 == null)
                return;
            record1.setPlayed(true);
            final AnimationDrawable animationDrawable = (AnimationDrawable) audioLayout.getBackground();
            //重置动画
            resetAnim(animationDrawable);
            //重置数据状态
            resetData();

            //处理点击正在播放的语音时，可以停止；再次点击时重新播放。
            if (record1.isPlaying()) {
                record1.setPlaying(false);
                MediaManager.release();//重置
            } else {
                record1.setPlaying(true);
                //获取语音信号栏，开始播放动画
                animationDrawable.start();
                //播放前重置。
                MediaManager.release();
                //开始实质播放
                MediaManager.playSound(record1.getPath(),
                        mp -> {
                            animationDrawable.selectDrawable(0);//显示动画第一帧
                            animationDrawable.stop();
                        });
            }
        });
    }

    private void deleteRecvMessage(final List<String> messageIds) {
        if (messageIds != null && messageIds.size() > 0) {
            String strUploading = getString(R.string.message_message_deleting);
            ProgressManager.getInstance().showProgress(this, null, strUploading, false);
            if (messageController == null)
                messageController = new MessageController(this);
//            List<MessageID> ids = new ArrayList<MessageID>();
//            for (String id : messageIds) {
//                MessageID messageID = new MessageID(id);
//                ids.add(messageID);
//            }
//            messageController.deleteRecvMsg(mUser.getToken(), ids, new MessageCallBack() {
//                @Override
//                public void onDeleteRecvMsg(boolean isSuccess, int errorCode, String errMsg) {
//                    super.onDeleteRecvMsg(isSuccess, errorCode, errMsg);
//                    ProgressManager.getInstance().cancelProgress();
//                    if (isSuccess) {
//                        EventBus.getDefault().post(new MessageDetailEvent(MESSAGE_DETAIL_REMOVED));
//                        showRemoveFinishedDialog();
//                    } else {
//                        toast(errMsg);
//                    }
//                }
//            });
        }
    }

    private void deleteSendMessage(final List<String> messageIds) {
        if (messageIds != null && messageIds.size() > 0) {
            String strUploading = getString(R.string.message_message_deleting);
            ProgressManager.getInstance().showProgress(this, null, strUploading, false);
            if (messageController == null)
                messageController = new MessageController(this);
//            List<MessageID> ids = new ArrayList<MessageID>();
//            for (String id : messageIds) {
//                MessageID messageID = new MessageID(id);
//                ids.add(messageID);
//            }
//            messageController.deleteSendMsg(mUser.getToken(), ids, new MessageCallBack() {
//                @Override
//                public void onDeleteSendMsg(boolean isSuccess, int errorCode, String errMsg) {
//                    super.onDeleteSendMsg(isSuccess, errorCode, errMsg);
//                    ProgressManager.getInstance().cancelProgress();
//                    if (isSuccess) {
//                        EventBus.getDefault().post(new MessageDetailEvent(OUT_MESSAGE_DETAIL_REMOVED));
//                        showRemoveFinishedDialog();
//                    } else {
//                        toast(errMsg);
//                    }
//                }
//            });
        }
    }

    private void showRemoveFinishedDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.message_remove_ok);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                UiMessageDetail.this.finish();
            }
        });
        builder.show();
    }

    public final static String MESSAGE_DETAIL_REMOVED = "messageDetailRemoved";
    public final static String OUT_MESSAGE_DETAIL_REMOVED = "outMessageDetailRemoved";

    public static class MessageDetailEvent {
        public String message;

        public MessageDetailEvent(String message) {
            this.message = message;
        }

    }

    private View getReplayView(MessageDeatilBean.MessageReplyBean messageReplyBean) {
        View rootView = LayoutInflater.from(this).inflate(R.layout.item_message_replay, null);
        final TextView name = (TextView) rootView.findViewById(R.id.tv_name);
        final TextView date = (TextView) rootView.findViewById(R.id.tv_date);
        final TextView num = (TextView) rootView.findViewById(R.id.tv_document_num);
        final TextView content = (TextView) rootView.findViewById(R.id.tv_message_content);
        final LinearLayout area = (LinearLayout) rootView.findViewById(R.id.layout_area);
        final LinearLayout document = (LinearLayout) rootView.findViewById(R.id.layout_document);
        final TextView csName = (TextView) rootView.findViewById(R.id.tv_cs_name);
        List<FilesBean> filesBeansReplay = new ArrayList<>();
        if (!TextUtils.isEmpty(messageReplyBean.getCreateUserName())) {
            name.setText(messageReplyBean.getCreateUserName());
        }
        if (!TextUtils.isEmpty(messageReplyBean.getCreateDate())) {
            date.setText(messageReplyBean.getCreateDate());
        }
        if (!TextUtils.isEmpty(messageReplyBean.getMessage())) {
            content.setText(messageReplyBean.getMessage());
        }
        if (!TextUtils.isEmpty(messageReplyBean.getRecipients())) {
            csName.setText(messageReplyBean.getRecipients());
        } else {
            csName.setText("暂无");
        }
        if (messageReplyBean.getPhotos() != null && messageReplyBean.getPhotos().size() > 0) {
            for (String photo : messageReplyBean.getPhotos()) {
                FilesBean filesBean = new FilesBean();
                filesBean.setFiletype("image");
                filesBean.setFilename(photo);
                filesBeansReplay.add(filesBean);
            }
        }
        if (messageReplyBean.getAudios() != null && messageReplyBean.getAudios().size() > 0) {
            for (String file : messageReplyBean.getAudios()) {
                FilesBean filesBean = new FilesBean();
                filesBean.setFiletype("audio");
                filesBean.setFilename(file);
                filesBeansReplay.add(filesBean);
            }

        }
        if (filesBeansReplay.size() > 0) {
            int totalNum = filesBeansReplay.size();
            for (FilesBean files : filesBeansReplay) {
                area.setVisibility(View.VISIBLE);
                View fileView = getViewFromFile(files, String.valueOf(messageReplyBean.getId() + 1000));
                document.addView(fileView);
            }
            num.setText(String.format("%d个附件", totalNum));
        } else {
            area.setVisibility(View.GONE);
        }
        return rootView;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x002) {
            initData();
            layoutMessageAddition.removeAllViews();
            layoutMessageReplay.removeAllViews();
        }
    }

    @Override
    protected void onDestroy() {
        BaseApp.cancelAllRequest(getClass().getName());
        //重置数据状态
        resetData();
        MediaManager.release();//重置
        super.onDestroy();
    }
}
