package com.ycgis.macall.personalcenter.v.fragment;

import android.content.Context;
import android.graphics.Point;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.bumptech.glide.Glide;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.rs.macall.androidx.basemodel.callback.CustomClickListener;
import com.rs.macall.androidx.basemodel.utils.DimensionConvert;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.FragmentFrequentlyQuestionsDetailsBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.FrequentlyItemBean;
import com.ycgis.macall.personalcenter.p.app.AppCache;
import com.ycgis.macall.personalcenter.v.photoview.ShowPhotoDialog;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FrequentlyQuestionsDetailsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FrequentlyQuestionsDetailsFragment extends BottomSheetDialogFragment {
    private FragmentFrequentlyQuestionsDetailsBinding binding;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public FrequentlyQuestionsDetailsFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @return A new instance of fragment FrequentlyQuestionsDetailsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FrequentlyQuestionsDetailsFragment newInstance(Serializable param1) {
        FrequentlyQuestionsDetailsFragment fragment = new FrequentlyQuestionsDetailsFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.BottomSheetDialog);
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        binding = FragmentFrequentlyQuestionsDetailsBinding.inflate(getLayoutInflater());
        init();
        return binding.getRoot();
    }

    private void init() {
    }

    private String photoUrl;
    @Override
    public void onStart() {
        super.onStart();
        // 设置软键盘不自动弹出
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
        FrameLayout bottomSheet = dialog.getDelegate().findViewById(R.id.design_bottom_sheet);
        if (bottomSheet != null) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
            layoutParams.height = getHeight();
            BottomSheetBehavior<FrameLayout> from = BottomSheetBehavior.from(bottomSheet);
//            behavior = BottomSheetBehavior.from(bottomSheet);
            // 初始为展开状态
            from.setState(BottomSheetBehavior.STATE_EXPANDED);
        }

        Bundle arguments = getArguments();
        if (arguments!=null){
            Serializable serializable = arguments.getSerializable(ARG_PARAM1);
            FrequentlyItemBean bean = (FrequentlyItemBean)serializable;
            binding.frequentlyTvExtend.setText(bean.getExtend());
            binding.frequentlyTvProblem.setText(bean.getProblem());
            String reply = bean.getReply();
            binding.frequentlyTvReply.setText(reply);

            String guidance = bean.getGuidance();
            if (StringUtil.isNullOrEmpty(guidance)){
                binding.frequentlyTvGuidance.setVisibility(View.GONE);
            }else {
                photoUrl =AppCache.BASE_IMAGE_URL+guidance;
                binding.frequentlyTvGuidance.setVisibility(View.VISIBLE);
                Glide.with(getContext()).load(photoUrl).into(binding.frequentlyTvGuidance);
                binding.frequentlyTvGuidance.setOnClickListener(new CustomClickListener() {
                    @Override
                    public void onSingleClick(View v) {
                        new ShowPhotoDialog(getContext(),R.style.MyDialogThemeBase).show(photoUrl);
                    }
                });
               /* int drawable = MResource.getIdByName(getContext(), "drawable", bean.getGuidance());
                if (drawable == 0){
                    binding.frequentlyTvGuidance.setVisibility(View.GONE);
                }else {
                    try {
                        Drawable drawable1 = ContextCompat.getDrawable(getContext(), drawable);
                        binding.frequentlyTvGuidance.setVisibility(View.VISIBLE);
                        binding.frequentlyTvGuidance.setImageDrawable(drawable1);
                        binding.frequentlyTvGuidance.setOnClickListener(new CustomClickListener() {
                            @Override
                            public void onSingleClick(View v) {
                                new ShowPhotoDialog(getContext()).show(binding.frequentlyTvGuidance.getDrawable());
                            }
                        });
                    }catch (Exception e){
                        e.printStackTrace();
                        binding.frequentlyTvGuidance.setVisibility(View.GONE);
                    }
                }*/
            }
        }
    }


    /**
     * 获取屏幕高度
     *
     * @return height
     */
    private int getHeight() {
        int height = 1920;
        if (getContext() != null) {
            WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
            Point point = new Point();
            if (wm != null) {
                // 使用Point已经减去了状态栏高度
                wm.getDefaultDisplay().getSize(point);
                height = point.y - DimensionConvert.dip2px(getContext(),120);
            }
        }
        return height;
    }

}