package com.tofly.ghrq_route.common.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.gaoyuan.weixinrecord.example.Record;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.uploadFile.domain.UploadFileBean;
import com.tofly.ghrq_route.module.uploadPhoto.domain.ReportedPhotoBean;
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.
 */

public class BottomSheetUploadPhotoDetail extends LinearLayout {
    @BindView(R.id.bottom_sheet_detail_header)
    RelativeLayout bottomSheetDetailHeader;
    @BindView(R.id.reporteddetail_user)
    TextView textReportUser;
    @BindView(R.id.reporteddetail_time)
    TextView textReportTime;
    @BindView(R.id.reporteddetail_phototype)
    TextView textPhtoType;
    @BindView(R.id.reporteddetail_photo_addr)
    TextView textPhotoAddr;
    @BindView(R.id.reporteddetail_detail)
    TextView textPhotoDetail;
    @BindView(R.id.gallery)
    LinearLayout imageGallery;
    @BindView(R.id.layout_audio_record)
    LinearLayout layoutAudioRecord;
    @BindView(R.id.audio_record_gallery)
    LinearLayout layoutAudioGallery;

    private Context mContext;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private BottomSheetBehavior bottomSheetBehavior = null;
    private boolean isBottomSheetDetailShow = false;
    private boolean isDialogMode = false;

    final int BOTTOM_SHEET_BOTTOM = BaseApp.dip2px(56);
    final int BOTTOM_SHEET_MIDDLE = BaseApp.dip2px(350);
    int BOTTOM_SHEET_TOP = ViewGroup.LayoutParams.MATCH_PARENT;

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

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

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

        mContext = context;
        fileViewUtil = new FileViewUtil(context);
        mRecords = new ArrayList<>();
        BOTTOM_SHEET_TOP = BaseUi.getDialogHeight(mContext) - BaseApp.dip2px(60);
        initAttrs(attrs);
        initView();
        initListener();
    }

    public boolean isBottomSheetDetailShowing() {
        return isBottomSheetDetailShow;
    }

    private void initAttrs(AttributeSet attrs){
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.BottomSheetRepairDetail);
        try {
            isDialogMode = typedArray.getBoolean(R.styleable.BottomSheetRepairDetail_DialogMode, false);
        }finally {
            typedArray.recycle();
        }
    }

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


    }

    private void initListener() {

    }

    public void hideSelf() {
        isBottomSheetDetailShow = false;
        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) this.getLayoutParams();
        if (layoutParams.height == BOTTOM_SHEET_TOP) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            this.postDelayed(new Runnable() {
                @Override
                public void run() {
                    BottomSheetUploadPhotoDetail.this.setVisibility(View.INVISIBLE);
                }
            }, 50);
        } else {
            this.setVisibility(View.INVISIBLE);
        }
    }

    public void setBottomDetailState(@BottomSheetBehavior.State int state) {
        if (bottomSheetBehavior != null) {
            if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED
                    && state == BottomSheetBehavior.STATE_COLLAPSED) {
                bottomSheetBehavior.setState(state);
            } else if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED
                    && state == BottomSheetBehavior.STATE_EXPANDED) {
                bottomSheetBehavior.setState(state);
            }
        }
    }

    public void initBottomSheetDetail(ReportedPhotoBean photoBean) {
        isBottomSheetDetailShow = true;

        if (isDialogMode) {
            bottomSheetDetailHeader.setOnClickListener(null);
        }

        if (!isDialogMode) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) this.getLayoutParams();
            layoutParams.height = BOTTOM_SHEET_MIDDLE;
            setLayoutParams(layoutParams);
            bottomSheetBehavior = BottomSheetBehavior.from(this);

            bottomSheetBehavior.setPeekHeight(BOTTOM_SHEET_BOTTOM);
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

            BOTTOM_SHEET_TOP = BaseUi.getDialogHeight(mContext) - BaseApp.dip2px(120);

            final float[] offset = {0};
            bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {
                    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
                    switch (newState) {
                        case BottomSheetBehavior.STATE_DRAGGING:
                            layoutParams.height = BOTTOM_SHEET_TOP;
                            bottomSheet.setLayoutParams(layoutParams);
                            break;
                        case BottomSheetBehavior.STATE_COLLAPSED:
                            break;
                        case BottomSheetBehavior.STATE_SETTLING:
                            if (offset[0] >= 0 && offset[0] <= 1) {
                                if (offset[0] > 0.7 && offset[0] < 1) {
                                    bottomSheetBehavior.setPeekHeight(BOTTOM_SHEET_BOTTOM);
                                    layoutParams.height = BOTTOM_SHEET_TOP;
                                    bottomSheet.setLayoutParams(layoutParams);
                                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                                } else if (offset[0] > 0.35 && offset[0] < 0.7) {
                                    bottomSheetBehavior.setPeekHeight(BOTTOM_SHEET_BOTTOM);
                                    layoutParams.height = BOTTOM_SHEET_MIDDLE;
                                    bottomSheet.setLayoutParams(layoutParams);
                                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
                                } else if (offset[0] != 0) {
                                    bottomSheetBehavior.setPeekHeight(BOTTOM_SHEET_BOTTOM);
                                    layoutParams.height = BOTTOM_SHEET_MIDDLE;
                                    bottomSheet.setLayoutParams(layoutParams);
                                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                                }
                            }
                            break;
                        case BottomSheetBehavior.STATE_EXPANDED:
                            break;
                    }
                }

                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                    offset[0] = slideOffset;
                }
            });

            bottomSheetDetailHeader.setOnClickListener(view -> {
                if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
            });
        }

        if (photoBean != null) {
            String userName = photoBean.getUploadUserName();
            textReportUser.setText(userName);
            String createTime = photoBean.getCreateTime();
            textReportTime.setText(createTime);
            String picTypeName = photoBean.getPictypeName();
            textPhtoType.setText(picTypeName);
            String address = photoBean.getAddress();
            textPhotoAddr.setText(address);
            String notes = photoBean.getDescription();
            textPhotoDetail.setText(notes);

            String schemeId = photoBean.getId();
            List<UploadFileBean> files = photoBean.getFiles();
            for (UploadFileBean fileBean : files) {
                String filePath = fileBean.getFilepath();
                String fileNote = fileBean.getFilenote();
                String filename = fileBean.getFilename();
                if (FileUtil.isFileTypeImage(filePath)) {
                    fileViewUtil.getImageFileView(filePath, (isSuccess, view) -> {
                        if (isSuccess && view != null) {
                            imageGallery.addView(view);
                        }
                    });
                } else if (FileUtil.isFileTypeVideo(filePath)) {
                    fileViewUtil.getVideoFileView(schemeId, filePath, filename, fileNote, (isSuccess, view) -> {
                        if (isSuccess && view != null) {
                            imageGallery.addView(view);
                        }
                    });
                } else if (FileUtil.isFileTypeAudio(filePath)) {
                    layoutAudioRecord.setVisibility(VISIBLE);
                    fileViewUtil.getAudioFileView(schemeId, filePath, 1, fileNote, mRecords, null, (isSuccess, view) -> {
                        if (isSuccess && view != null) {
                            layoutAudioGallery.addView(view);
                        }
                    });
                }
            }
        }
    }
}
