package com.unis.sxzcl.ui.basic;

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

import com.bumptech.glide.Glide;
import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.UrlPath;
import com.unis.model.UserModel;
import com.unis.model.bean.MultiDepotBean;
import com.unis.model.bean.person.PersonBean;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentPersonDetailBinding;
import com.unis.sxzcl.util.CallUtils;
import com.xyxsbj.mylibrary.tool.ETTool;
import com.xyxsbj.mylibrary.tool.ToastTool;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.api.widget.Widget;

import java.util.ArrayList;

/**
 * 包名: com.unis.sxzcl.ui.basic
 * 类名: PersonDetailFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/10 10:54
 * 更新人:
 * 更新时间: 2020/12/10 10:54
 * 更新说明:
 * 版本号: 1.0
 */
public class PersonDetailFragment extends BaseFragment<FragmentPersonDetailBinding> {

    private String mId, mHouseId;
    private String mHeadImg;

    public static PersonDetailFragment newFragment(boolean showTitle, String id, String houseId) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("showTitle", showTitle);
        bundle.putString("id", id);
        bundle.putString("houseId", houseId);
        PersonDetailFragment fragment = new PersonDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected FragmentPersonDetailBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentPersonDetailBinding.inflate(inflater, container, false);
    }


    @Override
    protected void init() {
        Bundle bundle = getArguments();

        boolean showTitle = bundle.getBoolean("showTitle", true);
        mBinding.bar.setVisibility(showTitle ? View.VISIBLE : View.GONE);

        mBinding.bar.setTitle("");
        mBinding.bar.setLeftClickListener(v -> onBack());

        mBinding.tvCall.setOnClickListener(v -> {
            String phone = ETTool.getData(mBinding.tvPhone);
            CallUtils.callPhone(getContext(), phone);
        });

        mBinding.ivShow.setOnClickListener(v -> {
            if (mHeadImg == null) return;
            showBigImg(mHeadImg);
        });
    }

    @Override
    protected void onFirstShow() {

        mId = getArguments().getString("id");
        mHouseId = getArguments().getString("houseId");
        if (mId == null && mHouseId == null) {
            ToastTool.showLong("没有参数");
            onBack();
            return;
        }
        showProgress("正在加载...", false);
        if (mId != null)
            UserModel.get().getPersonDetail(mId, mCallback);
        if (mHouseId != null) {
            MultiDepotBean depot = DepotModel.get().getSelectDepot();
            UserModel.get().getPersonDetailByHouse(depot.depotCode, mHouseId, mCallback);
        }
    }

    DataCallback<PersonBean> mCallback = new DataCallback<PersonBean>() {
        @Override
        public void onSuccess(PersonBean data) {
            if (!isActive()) return;
            hideProgress();

            if (data == null) {
                ToastTool.showLong("没有人员信息");
                return;
            }

            mBinding.bar.setTitle(data.getUname());

            mHeadImg = UrlPath.image_host + data.getPheadPortrait();

            Glide.with(getContext())
                    .load(mHeadImg)
                    .placeholder(R.drawable.ic_img_loading)
                    .error(R.drawable.ic_user_empty)
                    .into(mBinding.ivShow);
            mBinding.tvName.setText(data.getUname());
            String sex = data.getUsex();
            if (sex != null) {
                sex = sex.equals("0") ? "男" : "女";
            }
            mBinding.tvSex.setText(sex);
            mBinding.tvCompany.setText(data.getDepotName());
            mBinding.tvDeportment.setText(data.getDepartmentName());
            mBinding.tvPost.setText(data.getPpost());
            mBinding.tvPhone.setText(data.getUphone());
        }

        @Override
        public void onError(String error) {
            super.onError(error);
            if (!isActive()) return;
            hideProgress();
        }
    };

    private void showBigImg(String title, ArrayList<String> list) {
        Widget widget = Widget.newLightBuilder(getContext())
                .title(title).build();
        Album.gallery(getContext())
                .checkedList(list)
                .widget(widget)
                .start();
    }

    private void showBigImg(String path) {
        ArrayList<String> list = new ArrayList<>();
        list.add(path);
        showBigImg("头像", list);
    }


}
