package com.rdiot.yx485.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.rdiot.yx485.R;
import com.rdiot.yx485.base.BaseFragment;
import com.rdiot.yx485.bean.HomeResultBean;
import com.rdiot.yx485.databinding.FragmentHomeMemeberBinding;
import com.rdiot.yx485.http.Url;
import com.rdiot.yx485.util.DialogUtils;

public class HomeMemberFragment extends BaseFragment {
    HomeResultBean.HomeInfo.MemberBean mMember;
    private FragmentHomeMemeberBinding mBinding;
    private HomeViewModel mViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentHomeMemeberBinding.inflate(inflater, container, false);
        initView();
        initData();
        return mBinding.getRoot();
    }

    private void initView() {
        mBinding.tvRemark.setOnClickListener(
                v -> DialogUtils.showInputDialog(requireActivity(), "设置备注", mMember.remark, "请输入备注信息", remark ->
                        {
                            mViewModel.resetMemberRemark(mMember.record_id, remark);
                            mMember.remark = remark;
                        }
                ));
        mBinding.btnDelete.setOnClickListener(v -> {
            DialogUtils.showAlertDialog(requireContext(), "删除成功", "确定要删除改成员吗？", (dialog, which) -> {
                if (mMember == null) {
                    return;
                }

                if (getActivity() instanceof HomeCreateActivity) {
                    ((HomeCreateActivity) getActivity()).deleteHomeMember(mMember.home_id, mMember.user_id);
                }

            }, (dialog, which) -> {
            });
        });
    }

    private void initData() {
        mViewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
        mViewModel.getUpdateState().observe(getViewLifecycleOwner(), success -> {
            if (success) {
                mBinding.tvRemark.setText(mMember.remark);
                if (getActivity() instanceof HomeCreateActivity) {
                    ((HomeCreateActivity) getActivity()).mUpdateMemberInfo = true;
                }
            }
        });
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }

        mMember = bundle.getParcelable("member");
        if (mMember == null) {
            return;
        }

        if (!TextUtils.isEmpty(mMember.user_name)) {
            mBinding.tvName.setText(mMember.user_name);
        }

        if (!TextUtils.isEmpty(mMember.remark)) {
            mBinding.tvRemark.setText(mMember.remark);
        }

        if (!TextUtils.isEmpty(mMember.photo)) {
            Glide.with(this)
                    .load(Url.BASE_URL + mMember.photo)
                    .error(Glide.with(this).load(R.drawable.tx))
                    .into(mBinding.imageAvatar);
        }

        String adminId = bundle.getString("adminId", "");
        if (TextUtils.isEmpty(adminId)) {
            return;
        }

        if (adminId.equals(mMember.user_id)) {
            mBinding.tvDesc.setText(String.format("%s创建家庭", mMember.created_at));
        } else {
            mBinding.tvDesc.setText(String.format("%s加入该家庭", mMember.created_at));
        }
    }
}
