package com.rdiot.yx485.fragment;

import static com.rdiot.yx485.http.Url.BASE_URL2;

import android.content.Intent;
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.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.rdiot.yx485.CheckUpdateActivity;
import com.rdiot.yx485.InviteActivity;
import com.rdiot.yx485.PerInfoActivity;
import com.rdiot.yx485.R;
import com.rdiot.yx485.bean.HomeBase;
import com.rdiot.yx485.bean.PersonBean;
import com.rdiot.yx485.databinding.FragmentPersonBinding;
import com.rdiot.yx485.device.ConsumableActivity;
import com.rdiot.yx485.home.HomeActivity;
import com.rdiot.yx485.http.Url;
import com.rdiot.yx485.main.MainViewModel;
import com.rdiot.yx485.util.DialogUtils;
import com.rdiot.yx485.util.IntentUtils;
import com.rdiot.yx485.util.LocalCache;
import com.rdiot.yx485.webview.WebViewActivity;

import java.util.Objects;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PersonFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PersonFragment extends Fragment {
    private FragmentPersonBinding mBinding;
    private MainViewModel mViewModel;

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

    public static PersonFragment newInstance() {
        return new PersonFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        mViewModel.getUserInfo().observe(this, this::refreshView);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentPersonBinding.inflate(inflater, container, false);

        mBinding.layoutDefaultHomeInfo.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setClass(requireContext(), HomeActivity.class);
            startActivity(intent);
        });
        mBinding.message.setOnClickListener(view2 -> {
            Intent intent = new Intent();
            intent.setClass(requireContext(), CheckUpdateActivity.class);
            startActivity(intent);
        });
        mBinding.fankui.setOnClickListener(v -> WebViewActivity.newIntent(requireContext(), BASE_URL2 + "/#/helpList", null));
        mBinding.layoutConsumable.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(), ConsumableActivity.class);
            startActivity(intent);
        });
        mBinding.yaoqing.setOnClickListener(view4 -> {
            HomeBase homeBase = LocalCache.getDefaultHome();
            if (homeBase != null) {
                InviteActivity.newIntent(requireContext(), homeBase);
            }
        });
        mBinding.loginout.setOnClickListener(view1 -> showLogoutDialog());
        mBinding.layoutInfo.setOnClickListener(view3 -> {
            PerInfoActivity.newIntent(requireContext());
        });
        return mBinding.getRoot();
    }

    private void showLogoutDialog() {
        DialogUtils.showAlertDialog(requireContext(), "退出登录", "您确定要退出当前账号吗？",
                (dialog, which) -> logout(),
                (dialog, which) -> {
                });
    }

    private void logout() {
        LocalCache.logout();
        IntentUtils.intentToLogin(requireActivity());
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isResumed()) {
            mViewModel.loadUserInfo();
        }
    }

    private void refreshView(PersonBean.UserInfo info) {
        if (info == null) {
            return;
        }

        if (!TextUtils.isEmpty(info.user_name)) {
            mBinding.name.setText(info.user_name);
        }

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

        mBinding.team.setText(String.format("家庭  %s | 智能设备 %s", info.home_count, info.all_devices));
        mBinding.tvHomeName.setText(info.default_home_name);
        for (PersonBean.UserInfo.HomesBean homeInfo : info.homes) {
            if (Objects.equals(homeInfo.record_id, info.default_home_id)) {
                mBinding.roomNum.setText(String.format("房间 %s  | ", homeInfo.room == null ? 0 : homeInfo.room.size()));
                mBinding.deviceNum.setText(String.format("设备 %s  | ", homeInfo.devices == null ? 0 : homeInfo.devices.size()));
                mBinding.memberNum.setText(String.format("成员 %s ", homeInfo.member == null ? 0 : homeInfo.member.size()));
            }
        }
    }
}