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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gaoyuan.weixinrecord.example.Record;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.module.trouble.domain.AuditSchemeBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleProcessBean;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleSectaryVerify;
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.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.tofly.ghrq_route.base.ui.BaseUi.getDialogHeight;

/**
 * Created by Amu on 2017/6/19.
 */

public class TroubleSchemeDetailBottomDialog extends BottomSheetDialog {
    @BindView(R.id.text_duty_name)
    TextView textDutyName;
    @BindView(R.id.text_scheme_time)
    TextView textSchemeTime;
    @BindView(R.id.text_executor_name)
    TextView textExecutorName;
    @BindView(R.id.text_assists_name)
    TextView textAssistsName;
    @BindView(R.id.text_scheme_note)
    TextView textSchemeNote;
    @BindView(R.id.layout_scheme_note)
    LinearLayout layoutSchemeNote;
    @BindView(R.id.scheme_file_gallery)
    LinearLayout schemeFileGallery;
    @BindView(R.id.layout_scheme_file_area)
    LinearLayout layoutSchemeFileArea;
    @BindView(R.id.scheme_addition_gallery)
    LinearLayout schemeAdditionGallery;
    @BindView(R.id.layout_scheme_addition_area)
    LinearLayout layoutSchemeAdditionArea;
    @BindView(R.id.layout_scheme_file_addition_area)
    LinearLayout layoutSchemeFileAdditionArea;
    @BindView(R.id.layout_scheme_module)
    LinearLayout layoutSchemeModule;
    @BindView(R.id.btn_verify)
    Button btnVerify;
    @BindView(R.id.layout_action)
    LinearLayout layoutAction;
    @BindView(R.id.text_verify_user)
    TextView textVerifyUser;
    @BindView(R.id.text_check_time)
    TextView textCheckTime;
    @BindView(R.id.text_verify_state)
    TextView textVerifyState;
    @BindView(R.id.text_verify_note)
    TextView textVerifyNote;
    @BindView(R.id.layout_verify_note)
    LinearLayout layoutVerifyNote;
    @BindView(R.id.verify_addition_gallery)
    LinearLayout verifyAdditionGallery;
    @BindView(R.id.layout_verify_addition_area)
    LinearLayout layoutVerifyAdditionArea;
    @BindView(R.id.layout_check_report_module)
    LinearLayout layoutCheckReportModule;
    @BindView(R.id.btn_header_close)
    ImageView imageHeaderClose;
    @BindArray(R.array.trouble_scheme_files)
    String[] schemeFiles;

    private Context mContext;

    private TroubleBean mTroubleBean;

    private AuditSchemeBean mAuditSchemeBean;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private boolean mIsEditable = true;

    public TroubleSchemeDetailBottomDialog(@NonNull Context context, TroubleBean troubleBean, boolean isEditable, AuditSchemeBean auditSchemeBean) {
        super(context);
        mContext = context;
        mTroubleBean = troubleBean;
        mIsEditable = isEditable;
        mAuditSchemeBean = auditSchemeBean;
        fileViewUtil = new FileViewUtil(context);
        mRecords = new ArrayList<>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.item_trouble_scheme_verify_detail);
        ButterKnife.bind(this);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels); // 高度设置为屏幕的0.6
        lp.height = (int) (d.heightPixels);
        dialogWindow.setAttributes(lp);

        initView();
        initData();
    }

    @Override
    public void show() {
        super.show();
        //设置高度
        FrameLayout bottomSheet = findViewById(R.id.design_bottom_sheet);
        ViewGroup.LayoutParams originLayoutParams = bottomSheet.getLayoutParams();
        originLayoutParams.height = getDialogHeight(mContext);
        bottomSheet.setLayoutParams(originLayoutParams);
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

    private void initView() {
        setContentView(mAuditSchemeBean);
    }

    private void initData() {

    }

    public void setContentView(AuditSchemeBean schemeBean) {
        mAuditSchemeBean = schemeBean;
        if (mTroubleBean != null) {
            String dutyUserName = mTroubleBean.getDutyUserName();
            if (dutyUserName == null || dutyUserName.isEmpty()) {
                dutyUserName = mTroubleBean.getUndertakerUserName();
            }
            if (dutyUserName != null) {
                textDutyName.setText(dutyUserName);
            }
        }

        if (schemeBean != null) {
            textExecutorName.setText(schemeBean.getHandleUserName());

            String coProcessor = schemeBean.getCoprocessorName();
            if (coProcessor != null && !coProcessor.isEmpty()) {
                textAssistsName.setText(coProcessor);
            }
            String dutyCheckTime = schemeBean.getDutyCheckTime();
            if (dutyCheckTime != null)
                textSchemeTime.setText(dutyCheckTime);

            layoutSchemeNote.setVisibility(View.GONE);
            String dutyPlanNote = schemeBean.getDutyPlanNote();
            if (dutyPlanNote != null && !dutyPlanNote.isEmpty()) {
                layoutSchemeNote.setVisibility(View.VISIBLE);
                textSchemeNote.setText(dutyPlanNote);
            }

            String schemeId = schemeBean.getId();
            String fileNote = schemeFiles[0];
            schemeFileGallery.removeAllViews();
            List<AuditFileBean> rectifyFile = schemeBean.getRectifyFile();               ///整改方案
            for (AuditFileBean auditFileBean : rectifyFile) {
                getSchemeFiles(schemeId, auditFileBean, fileNote, true, schemeFileGallery);
            }
            fileNote = schemeFiles[1];
            List<AuditFileBean> protectNoticeFile = schemeBean.getProtectNoticeFile();   ///管道保护书
            for (AuditFileBean auditFileBean : protectNoticeFile) {
                getSchemeFiles(schemeId, auditFileBean, fileNote,true, schemeFileGallery);
            }
            fileNote = schemeFiles[2];
            List<AuditFileBean> rectifyNoticeFile = schemeBean.getRectifyNoticeFile();   ///整改通知书
            for (AuditFileBean auditFileBean : rectifyNoticeFile) {
                getSchemeFiles(schemeId, auditFileBean, fileNote, true, schemeFileGallery);
            }
            fileNote = schemeFiles[3];
            List<AuditFileBean> rectifyFinalFile = schemeBean.getRectifyFinalFile();    ///整改决算书
            for (AuditFileBean auditFileBean : rectifyFinalFile) {
                getSchemeFiles(schemeId, auditFileBean, fileNote, true, schemeFileGallery);
            }
            if (rectifyFile.size() > 0 || protectNoticeFile.size() > 0
                    || rectifyNoticeFile.size() > 0 || rectifyFinalFile.size() > 0 ) {
                layoutSchemeFileArea.setVisibility(View.VISIBLE);
            } else {
                layoutSchemeFileArea.setVisibility(View.GONE);
            }

            fileNote = "附件";
            schemeAdditionGallery.removeAllViews();
            List<AuditFileBean> schemeFiles = schemeBean.getSchemeFiles();    ///附件
            for (AuditFileBean auditFileBean : schemeFiles) {
                getSchemeFiles(schemeId, auditFileBean, fileNote, false, schemeAdditionGallery);
            }
            List<AuditFileBean> schemeVoice = schemeBean.getSchemeVoice();    ///音频
            for (AuditFileBean auditFileBean : schemeVoice) {
                getSchemeFiles(schemeId, auditFileBean, fileNote, false, schemeAdditionGallery);
            }

            if (schemeFiles.size() > 0 || schemeVoice.size() > 0) {
                layoutSchemeAdditionArea.setVisibility(View.VISIBLE);
            } else {
                layoutSchemeAdditionArea.setVisibility(View.GONE);
            }

            String auditMindName = schemeBean.getAuditMindName();
            if (auditMindName == null) {
                boolean needVerifyScheme = checkNeedVerifyScheme();
                if (mIsEditable && needVerifyScheme) {
                    layoutAction.setVisibility(View.VISIBLE);
                }
                layoutCheckReportModule.setVisibility(View.GONE);
            } else {
                layoutAction.setVisibility(View.GONE);
                layoutCheckReportModule.setVisibility(View.VISIBLE);

                textVerifyUser.setText(schemeBean.getAuditorSecretaryName());
                String auditTime = schemeBean.getAuditTime();
                if (auditTime != null)
                    textCheckTime.setText(auditTime);

                layoutVerifyNote.setVisibility(View.GONE);
                String description = schemeBean.getAuditDescription();
                if (description != null && !description.isEmpty()) {
                    layoutVerifyNote.setVisibility(View.VISIBLE);
                    textVerifyNote.setText(description);
                }

                textVerifyState.setText(auditMindName);

                fileNote = "附件";
                verifyAdditionGallery.removeAllViews();
                List<AuditFileBean> auditSchemeFiles = schemeBean.getAuditSchemeFiles();    ///附件
                for (AuditFileBean auditFileBean : auditSchemeFiles) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, false, verifyAdditionGallery);
                }
                List<AuditFileBean> auditSchemeVoice = schemeBean.getAuditSchemeVoice();    ///音频
                for (AuditFileBean auditFileBean : auditSchemeVoice) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, false, verifyAdditionGallery);
                }

                if (auditSchemeFiles.size() > 0 || auditSchemeVoice.size() > 0) {
                    layoutVerifyAdditionArea.setVisibility(View.VISIBLE);
                } else {
                    layoutVerifyAdditionArea.setVisibility(View.GONE);
                }
            }
        }
    }

    private boolean checkNeedVerifyScheme() {
        boolean hasModule = false;
        List<TroubleProcessBean> checks = mTroubleBean.getChecks();
        for (TroubleProcessBean check : checks) {
            if (check.getProcessId() == TroubleBean.TROUBLE_PROCESS_NEED_UNDER_SECTARY_VERIFY_SCHEME) {
                hasModule = true;
                break;
            }
        }
        return hasModule;
    }

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

    private View getViewWithName(View childView, String fileNote) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        final View rootView = inflater.inflate(R.layout.item_file_with_name, null);
        final FrameLayout layoutFile = rootView.findViewById(R.id.layout_file_area);
        final TextView textFileNote = rootView.findViewById(R.id.text_file_note);
        textFileNote.setText(fileNote);
        layoutFile.addView(childView);
        return rootView;
    }

    @OnClick({R.id.btn_header_close, R.id.btn_verify})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_header_close:{
                dismiss();
            }
            break;
            case R.id.btn_verify:{
                gotoUiSectaryVerify();
            }
            break;
        }
    }

    private void gotoUiSectaryVerify() {
        this.dismiss();

        Intent intent = new Intent();
        intent.setClass(mContext, UiTroubleSectaryVerify.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(TroubleBean.TROUBLE_BEAN_KEY, mTroubleBean);
        intent.putExtras(bundle);
        Activity activity = (Activity) mContext;
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.anim_rightin, R.anim.anim_leftout);
    }
}
