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 EmergencyRepairHandleInfoModuleLayout extends LinearLayout {
    @BindView(R.id.text_reporter)
    TextView textReporter;
    @BindView(R.id.text_report_time)
    TextView textReportTime;
    @BindView(R.id.text_repair_note)
    TextView textRepairNote;
    @BindView(R.id.layout_repair_note)
    LinearLayout layoutRepairNote;
    @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 EmergencyRepairHandleInfoModuleLayout(Context context) {
        this(context, null);
    }

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

    public EmergencyRepairHandleInfoModuleLayout(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_repair_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 responseUser = emergencyData.getResponsibleUserName();
        textReporter.setText(responseUser);

        /*上报时间*/
        String createTime = emergencyData.getCompleteTime();
        textReportTime.setText(createTime);

        /*抢修派工描述*/
        String maintenanceNotes = emergencyData.getMaintenanceNotes();
        if (maintenanceNotes != null && !maintenanceNotes.isEmpty()) {
            layoutRepairNote.setVisibility(VISIBLE);
            textRepairNote.setText(maintenanceNotes);
        }else {
            layoutRepairNote.setVisibility(GONE);
        }

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

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

        List<AuditFileBean> maintenanceVoices = emergencyData.getMaintenanceVoice();  ///附件
        if (maintenanceVoices != null) {
            fileCount = fileCount + maintenanceVoices.size();
            for (AuditFileBean auditFileBean : maintenanceVoices) {
                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);
            }
        }
    }
}
