package com.rdiot.yx485.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.rdiot.yx485.base.BaseFragmentActivity;
import com.rdiot.yx485.bean.CreateHomeBean;
import com.rdiot.yx485.bean.DefHomeRoomBean;
import com.rdiot.yx485.bean.HomeResultBean;

import java.util.ArrayList;
import java.util.List;

public class HomeCreateActivity extends BaseFragmentActivity {
    private final HomeViewModel mViewModel = new HomeViewModel();
    protected boolean mUpdateMemberInfo = false, mInitHomeAddress = false;
    protected String province = "", city = "", district = "", address = "";
    private boolean mUpdateAddress = false;

    /**
     * 将树形结构数据转换为三级联动数据
     */
    public static void convertToPickerData(List<DefHomeRoomBean.DataBean.CityInfo> regions,
                                           List<String> provinces,
                                           List<List<String>> cities,
                                           List<List<List<String>>> districts) {
        for (DefHomeRoomBean.DataBean.CityInfo province : regions) {
            provinces.add(province.name);
            List<String> cityList = new ArrayList<>();
            List<List<String>> districtList = new ArrayList<>();

            for (DefHomeRoomBean.DataBean.CityInfo city : province.children) {
                cityList.add(city.name);
                List<String> districtNames = new ArrayList<>();
                for (DefHomeRoomBean.DataBean.CityInfo district : city.children) {
                    districtNames.add(district.name);
                }
                districtList.add(districtNames);
            }

            cities.add(cityList);
            districts.add(districtList);
        }
    }

    public static Intent updateAddress(Context context, HomeResultBean.HomeInfo homeInfo) {
        Intent intent = new Intent(context, HomeCreateActivity.class);
        intent.putExtra("updateAddress", true);
        intent.putExtra("homeInfo", homeInfo);
        return intent;
    }

    public static Intent showMemberInfo(Context context, HomeResultBean.HomeInfo.MemberBean member, String adminId) {
        Intent intent = new Intent(context, HomeCreateActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("member", member);
        bundle.putString("adminId", adminId);
        intent.putExtra("bundle", bundle);
        return intent;
    }

    public static void initHomeAddress(Context context) {
        Intent intent = new Intent(context, HomeCreateActivity.class);
        intent.putExtra("setAddress", true);
        context.startActivity(intent);
    }

    @Override
    public void initTitleBar() {
        mBinding.llTitleBar.tvTitle.setText("新建家庭");
        mBinding.llTitleBar.btnTitleLeft.setVisibility(View.VISIBLE);
    }

    @Override
    public void initView() {
        mUpdateAddress = getIntent().getBooleanExtra("updateAddress", false);
        if (mUpdateAddress) {
            HomeResultBean.HomeInfo info = getIntent().getParcelableExtra("homeInfo");
            if (info != null) {
                province = info.province;
                city = info.city;
                district = info.district;
                address = info.address;
            }
            changeFragment(new HomeAddressFragment());
            mBinding.llTitleBar.tvTitle.setText("修改地址");
            return;
        }

        mInitHomeAddress = getIntent().getBooleanExtra("setAddress", false);
        if (mInitHomeAddress) {
            changeFragment(new HomeAddressFragment());
            mBinding.llTitleBar.tvTitle.setText("设置地址");
            return;
        }

        Bundle bundle = getIntent().getBundleExtra("bundle");
        if (bundle != null) {
            HomeMemberFragment fragment = new HomeMemberFragment();
            fragment.setArguments(bundle);
            changeFragment(fragment);
            mBinding.llTitleBar.tvTitle.setText("成员信息");
            return;
        }

        changeFragment(new HomeCreateFragment());
    }

    @Override
    public void initData() {
        mViewModel.getErrorMessage().observe(this, this::showMessage);
        mViewModel.getCreateState().observe(this, success -> {
            if (success) {
                showMessage("创建成功");
                onBackPressed();
            }
        });
        mViewModel.getUpdateState().observe(this, success -> {
            if (success) {
                showMessage("设置成功");
                onBackPressed();
            }
        });
        mViewModel.getDeleteState().observe(this, success -> {
            if (success) {
                showMessage("删除成功");
                mUpdateMemberInfo = true;
                onBackPressed();
            }
        });
    }

    protected void selectAddress() {
        changeFragment(new HomeAddressFragment());
    }

    protected void setAddress(String p, String c, String d, String a) {
        if (mInitHomeAddress) {
            mViewModel.initHomeAddress(p, c, d, a);
            return;
        }
        province = p;
        city = c;
        district = d;
        address = a;
        onBackPressed();
    }

    protected String getAddress() {
        return String.format("%s%s%s%s", province, city, district, address);
    }

    protected void createHome(CreateHomeBean bean) {
        if (TextUtils.isEmpty(province) || TextUtils.isEmpty(city) || TextUtils.isEmpty(district)) {
            showMessage("请选择地址");
            return;
        }

        bean.province = province;
        bean.city = city;
        bean.district = district;
        bean.address = address;
        mViewModel.createHome(bean);
    }

    protected void deleteHomeMember(String homeId, String id) {
        if (TextUtils.isEmpty(homeId) || TextUtils.isEmpty(id)) {
            showMessage("家庭ID和成员id不能为空");
            return;
        }

        mViewModel.deleteHomeMember(homeId, id);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("changed", mUpdateMemberInfo);

        if (mUpdateAddress) {
            Bundle bundle = new Bundle();
            bundle.putStringArray("address", new String[]{province, city, district, address});
            intent.putExtra("bundle", bundle);
        }
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }
}
