package com.tofly.ghrq_route.module.trouble.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gaoyuan.weixinrecord.example.Record;
import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBaseBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.common.ui.activity.UiCheckAddr;
import com.tofly.ghrq_route.common.util.Location.LocationUtil_xu;
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/1/10.
 */

public class TroubleReportAdapter extends ARecyclerBaseAdapter<TroubleReportAdapter.ViewHolder, TroubleBaseBean> {
    private Context context;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private TroubleBean mTroubleBean;

    public TroubleReportAdapter(Context context, TroubleBean troubleBean, List<TroubleBaseBean> reportList) {
        super(context, reportList);
        this.context = context;
        mTroubleBean = troubleBean;
        fileViewUtil = new FileViewUtil(context);
        mRecords = new ArrayList<>();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_trouble_report_info, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(position);
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text_count)
        TextView textCount;
        @BindView(R.id.text_addr)
        TextView textAddr;
        @BindView(R.id.layout_device)
        LinearLayout layoutDevice;
        @BindView(R.id.text_device)
        TextView textDevice;
        @BindView(R.id.layout_device_addr)
        LinearLayout layoutDeviceAddr;
        @BindView(R.id.text_reporter)
        TextView textReporter;
        @BindView(R.id.text_report_time)
        TextView textReportTime;
        @BindView(R.id.layout_trouble_name)
        LinearLayout layoutTroubleName;
        @BindView(R.id.text_trouble_name)
        TextView textTroubleName;
        @BindView(R.id.layout_unit_name)
        LinearLayout layoutUnitName;
        @BindView(R.id.text_unit_name)
        TextView textUnitName;
        @BindView(R.id.img_new)
        ImageView imgNew;
        @BindView(R.id.text_pipe_type)
        TextView textPipeType;
        @BindView(R.id.layout_pipe_type)
        LinearLayout layoutPipeType;
        @BindView(R.id.text_trouble_type)
        TextView textTroubleType;
        @BindView(R.id.layout_trouble_type)
        LinearLayout layoutTroubleType;
        @BindView(R.id.text_trouble_level)
        TextView textTroubleLevel;
        @BindView(R.id.layout_trouble_level)
        LinearLayout layoutTroubleLevel;
        @BindView(R.id.text_self_handle)
        TextView textSelfHandle;
        @BindView(R.id.layout_is_audit)
        LinearLayout layoutIsAudit;
        @BindView(R.id.text_is_audit)
        TextView textIsAudit;
        @BindView(R.id.layout_trouble_info)
        LinearLayout layoutTroubleInfo;
        @BindView(R.id.text_trouble_info)
        TextView textTroubleInfo;
        @BindView(R.id.addition_gallery)
        LinearLayout additionGallery;
        @BindView(R.id.layout_addition_area)
        LinearLayout layoutAdditionArea;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

            if (null != onItemClickListener) {
                itemView.setOnClickListener(v -> onItemClickListener.onItemClick(itemView, getLayoutPosition()));
            }
        }

        public void setData(final int position) {
            TroubleBaseBean baseBean = getItem(position);
            if (baseBean != null) {
                textCount.setText(position + 1 + "");
                textAddr.setText(baseBean.getAddress());

                String findTime = baseBean.getReportTime();
                if (findTime != null && !findTime.isEmpty()) {
                    textReportTime.setText(findTime);
                }

                String reportUserName = mTroubleBean.getReportUserName();
                if (reportUserName != null && !reportUserName.isEmpty()) {
                    textReporter.setText(reportUserName);
                }

                String troubleName = baseBean.getTroubleName();
                if (troubleName != null && !troubleName.isEmpty()) {
                    layoutTroubleName.setVisibility(View.VISIBLE);
                    textTroubleName.setText(troubleName);
                }else {
                    layoutTroubleName.setVisibility(View.GONE);
                }

                String unitName = baseBean.getUnitName();
                if (unitName != null && !unitName.isEmpty()) {
                    layoutUnitName.setVisibility(View.VISIBLE);
                    textUnitName.setText(unitName);
                }else {
                    layoutUnitName.setVisibility(View.GONE);
                }

                String troubleInfo = baseBean.getTroubleInfo();
                if (troubleInfo != null && !troubleInfo.isEmpty()) {
                    layoutTroubleInfo.setVisibility(View.VISIBLE);
                    textTroubleInfo.setText(troubleInfo);
                }else {
                    layoutTroubleInfo.setVisibility(View.GONE);
                }

                String pipeTypeName = baseBean.getLayerName();
                if (pipeTypeName != null && !pipeTypeName.isEmpty()) {
                    layoutPipeType.setVisibility(View.VISIBLE);
                    textPipeType.setText(pipeTypeName);
                }else {
                    layoutPipeType.setVisibility(View.GONE);
                }

                String troubleTypeName = baseBean.getTroubleTypeName();
                if (troubleTypeName != null && !troubleTypeName.isEmpty()) {
                    layoutTroubleType.setVisibility(View.VISIBLE);
                    textTroubleType.setText(troubleTypeName);
                }else {
                    layoutTroubleType.setVisibility(View.GONE);
                }

                String levelName = baseBean.getLevelName();
                if (levelName != null && !levelName.isEmpty()) {
                    layoutTroubleLevel.setVisibility(View.VISIBLE);
                    textTroubleLevel.setText(levelName);
                }else {
                    layoutTroubleLevel.setVisibility(View.GONE);
                }

                /*是否自行处理*/
                String discretionName = baseBean.getIsDiscretionName();
                textSelfHandle.setText(discretionName);

                String fileNote = "附件";
                String schemeId = baseBean.getId();
                additionGallery.removeAllViews();
                List<AuditFileBean> auditSchemeFiles = baseBean.getReportFile();    ///附件
                for (AuditFileBean auditFileBean : auditSchemeFiles) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, additionGallery);
                }
                List<AuditFileBean> auditSchemeVoice = baseBean.getReportVoiceList();    ///音频
                for (AuditFileBean auditFileBean : auditSchemeVoice) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, additionGallery);
                }

                layoutDevice.setOnClickListener(view -> {
                    LocationUtil_xu.CoordinateClass coordinate = null;
                    if (mTroubleBean.getLocationX() != null && mTroubleBean.getLocationY() != null) {
                        double lng = Double.parseDouble(mTroubleBean.getLocationX());
                        double lat = Double.parseDouble(mTroubleBean.getLocationY());
                        coordinate = new LocationUtil_xu.CoordinateClass(lng, lat, mTroubleBean.getAddress());
                    }
                    Activity activity = (Activity) mContext;
                    UiCheckAddr.startActivityForResult(activity,
                            UiCheckAddr.SELECT_MODE.TROUBLE_SELECT_ADDR_CHECK, coordinate);
                });
            }
        }
    }

    private void getSchemeFiles(String schemeId, AuditFileBean fileBean, String fileNote, final ViewGroup rootView) {
        if (fileBean != null) {
            String fileName = fileBean.getFileName();
            String filePath = fileBean.getPath();
            FileViewUtil.GetViewCallback getViewCallback = (isSuccess, view) -> {
                if (isSuccess && view != null) {
                    rootView.addView(view);
                }
            };
            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);
            }
        }
    }
}
