package com.tofly.ghrq_route.module.emergencyHandle.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gaoyuan.weixinrecord.example.Record;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.common.util.file.FileUtil;
import com.tofly.ghrq_route.common.util.file.FileViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/2/21.
 */

@SuppressLint("NonConstantResourceId")
public class EmergencyDispatchInfoModuleLayout extends LinearLayout {
    @BindView(R.id.text_response_user)
    TextView textResponseUser;
    @BindView(R.id.text_repair_department)
    TextView textRepairDepart;
    @BindView(R.id.text_repair_users)
    TextView textRepairUsers;
    @BindView(R.id.text_dispatch_info)
    TextView textDispatchInfo;
    @BindView(R.id.layout_dispatch_info)
    LinearLayout layoutDispatchInfo;
    @BindView(R.id.addition_gallery)
    LinearLayout additionGallery;
    @BindView(R.id.layout_addition_area)
    LinearLayout layoutAdditionArea;

    private final Context mContext;

    private final FileViewUtil fileViewUtil;

    private EmergencyHandleData mEmergencyHandleData;

    private final List<Record> mRecords;

    public EmergencyDispatchInfoModuleLayout(Context context) {
        this(context, null);
    }

    public EmergencyDispatchInfoModuleLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmergencyDispatchInfoModuleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;
        fileViewUtil = new FileViewUtil(context);
        mRecords = new ArrayList<>();

        initView();
        initListener();
    }

    private void initView() {
        inflate(mContext, R.layout.layout_emergency_dispatch_info_module, this);
        ButterKnife.bind(this);
    }

    private void initListener() {

    }

    public void initEmergencyInfo(EmergencyHandleData emergencyData) {
        if (emergencyData != null) {
            mEmergencyHandleData = emergencyData;
            setViewContent(emergencyData);
        }
    }

    private void setViewContent(EmergencyHandleData emergencyData) {
        /*负责人*/
        String responseUserName = emergencyData.getResponsibleUserName();
        textResponseUser.setText(responseUserName);

        /*抢修部门*/
        String repairDepartment = emergencyData.getMdispatchingDepartment();
        textRepairDepart.setText(repairDepartment);

        /*抢修人员*/
        String repairUsers = emergencyData.getMdispatchingUserName();
        textRepairUsers.setText(repairUsers);

        /*抢修派工描述*/
        String dispatchingNotes = emergencyData.getMdispatchingNotes();
        if (dispatchingNotes != null && !dispatchingNotes.isEmpty()) {
            layoutDispatchInfo.setVisibility(VISIBLE);
            textDispatchInfo.setText(dispatchingNotes);
        }else {
            layoutDispatchInfo.setVisibility(GONE);
        }

        /*附件*/
        String emergencyId = emergencyData.getId() + "";
        String fileNote = "附件";

        additionGallery.removeAllViews();
        List<AuditFileBean> dispatchingFiles = emergencyData.getMdispatchingFile();  ///附件
        int fileCount = 0;
        if (dispatchingFiles != null) {
            fileCount = dispatchingFiles.size();
            for (AuditFileBean auditFileBean : dispatchingFiles) {
                getSchemeFiles(emergencyId, auditFileBean, fileNote, additionGallery);
            }
        }

        if (fileCount > 0) {
            layoutAdditionArea.setVisibility(View.VISIBLE);
        } else {
            layoutAdditionArea.setVisibility(View.GONE);
        }
    }

    private void getSchemeFiles(String schemeId, AuditFileBean fileBean, String fileNote, final ViewGroup rootView) {
        if (fileBean != null) {
            FileViewUtil.GetViewCallback getViewCallback = (isSuccess, view) -> {
                if (isSuccess && view != null) {
                    rootView.addView(view);
                }
            };

            String fileName = fileBean.getFileName();
            String filePath = fileBean.getPath();
            if (FileUtil.isFileTypeImage(fileName)) {
                fileViewUtil.getImageFileView(filePath, getViewCallback);
            } else if (FileUtil.isFileTypeAudio(fileName)) {
                fileViewUtil.getAudioFileView(schemeId, filePath, 1, fileNote, mRecords, null, getViewCallback);
            } else {
                fileViewUtil.getFileView(schemeId, filePath, fileName, fileNote, getViewCallback);
            }
        }
    }
}
