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.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
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.AuditEndBean;
import com.tofly.ghrq_route.module.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.trouble.ui.UiTroubleUnderSectaryVerifyHandle;
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;
import butterknife.OnClick;

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

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

public class TroubleExecuteDetailBottomDialog extends BottomSheetDialog {
    @BindView(R.id.text_duty_name)
    TextView textDutyName;
    @BindView(R.id.text_scheme_time)
    TextView textSchemeTime;
    @BindView(R.id.text_scheme_note)
    TextView textSchemeNote;
    @BindView(R.id.layout_scheme_note)
    LinearLayout layoutSchemeNote;
    @BindView(R.id.scheme_addition_gallery)
    LinearLayout schemeAdditionGallery;
    @BindView(R.id.layout_scheme_addition_area)
    LinearLayout layoutSchemeAdditionArea;
    @BindView(R.id.scheme_audio_gallery)
    LinearLayout schemeAudioGallery;
    @BindView(R.id.layout_scheme_audio_area)
    LinearLayout layoutSchemeAudioArea;
    @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.verify_audio_gallery)
    LinearLayout verifyAudioGallery;
    @BindView(R.id.layout_verify_audio_area)
    LinearLayout layoutVerifyAudioArea;
    @BindView(R.id.layout_verify_addition_area)
    LinearLayout layoutVerifyAdditionArea;
    @BindView(R.id.layout_check_report_module)
    LinearLayout layoutCheckReportModule;

    private Context mContext;

    private TroubleBean mTroubleBean;

    private AuditEndBean mAuditEndBean;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private boolean mIsEditable = true;

    public TroubleExecuteDetailBottomDialog(@NonNull Context context, TroubleBean troubleBean, boolean isEditable, AuditEndBean auditEndBean) {
        super(context);
        mContext = context;
        mTroubleBean = troubleBean;
        mIsEditable = isEditable;
        mAuditEndBean = auditEndBean;
        fileViewUtil = new FileViewUtil(context);
        mRecords = new ArrayList<>();
    }

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

        setContentView(R.layout.item_trouble_execute_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 mDialogBehavior = BottomSheetBehavior.from(bottomSheet);
        mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

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

    private void initData() {

    }

    public void setContentView(AuditEndBean auditEndBean) {
        mAuditEndBean = auditEndBean;
        if (auditEndBean != null) {
            String dutyUserName = mTroubleBean.getDutyUserName();
            if (dutyUserName != null && !dutyUserName.isEmpty()) {
                textDutyName.setText(dutyUserName);
            }

            String handleTime = auditEndBean.getHandleTime();
            if (handleTime != null)
                textSchemeTime.setText(handleTime);

            layoutSchemeNote.setVisibility(View.GONE);
            String handleNote = auditEndBean.getHandleNote();
            if (handleNote != null && !handleNote.isEmpty()) {
                layoutSchemeNote.setVisibility(View.VISIBLE);
                textSchemeNote.setText(handleNote);
            }

            String auditEndId = auditEndBean.getId();
            String fileNote = "附件";
            schemeAdditionGallery.removeAllViews();
            List<AuditFileBean> schemeFiles = auditEndBean.getEndFiles();    ///附件
            for (AuditFileBean auditFileBean : schemeFiles) {
                getSchemeFiles(auditEndId, auditFileBean, fileNote, schemeAdditionGallery);
            }
            schemeAudioGallery.removeAllViews();
            List<AuditFileBean> schemeVoice = auditEndBean.getEndVoice();    ///音频
            for (AuditFileBean auditFileBean : schemeVoice) {
                getSchemeFiles(auditEndId, auditFileBean, fileNote, schemeAudioGallery);
            }

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

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

            String auditMindName = auditEndBean.getAuditMindName();
            if (auditMindName == null) {
                if (mIsEditable) {
                    layoutAction.setVisibility(View.VISIBLE);
                }
                layoutCheckReportModule.setVisibility(View.GONE);
            } else {
                layoutAction.setVisibility(View.GONE);
                layoutCheckReportModule.setVisibility(View.VISIBLE);

                textVerifyUser.setText(auditEndBean.getAuditorUndersecretaryName());
                String auditTime = auditEndBean.getAuditTime();
                if (auditTime != null)
                    textCheckTime.setText(auditTime);

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

                textVerifyState.setText(auditMindName);

                fileNote = "附件";
                verifyAdditionGallery.removeAllViews();
                List<AuditFileBean> auditSchemeFiles = auditEndBean.getAuditEndFiles();    ///附件
                for (AuditFileBean auditFileBean : auditSchemeFiles) {
                    getSchemeFiles(auditEndId, auditFileBean, fileNote, verifyAdditionGallery);
                }
                verifyAudioGallery.removeAllViews();
                List<AuditFileBean> auditSchemeVoice = auditEndBean.getAuditEndVoice();    ///音频
                for (AuditFileBean auditFileBean : auditSchemeVoice) {
                    getSchemeFiles(auditEndId, auditFileBean, fileNote, verifyAudioGallery);
                }

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

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

    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);
            }
        }
    }

    @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, UiTroubleUnderSectaryVerifyHandle.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);
    }
}
