package com.halo.assistant.fragment.user;

import android.app.Dialog;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.text.TextUtils;

import com.gh.common.util.DialogUtils;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.R;
import com.gh.gamecenter.login.entity.UserInfoEntity;
import com.gh.gamecenter.common.retrofit.ApiResponse;
import com.gh.gamecenter.login.user.UserViewModel;
import com.gh.gamecenter.common.base.fragment.ToolbarFragment;
import com.halo.assistant.fragment.user.region.OnRegionSelectListener;
import com.halo.assistant.fragment.user.region.RegionCountFragment;
import com.halo.assistant.fragment.user.region.RegionProvFragment;
import com.lightgame.listeners.OnBackPressedListener;

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

/**
 * Created by CsHeng on 19/12/2017.
 * <p>
 * 修改用户归属地
 */
public class SelectRegionFragment extends ToolbarFragment implements OnBackPressedListener, OnRegionSelectListener {

    private String mProvinceName;
    private RegionProvFragment mProvFragment;

    private UserViewModel mUserViewModel;

    private Dialog mLoadingDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.layout_wrapper_fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        changeFragment(null, null);

        UserViewModel.Factory factory = new UserViewModel.Factory(getActivity().getApplication());
        mUserViewModel = ViewModelProviders.of(this, factory).get(UserViewModel.class);
        mUserViewModel.getEditObsUserinfo().observe(this, new Observer<ApiResponse<UserInfoEntity>>() {
            @Override
            public void onChanged(@Nullable ApiResponse<UserInfoEntity> userInfoEntity) {
                if (mLoadingDialog != null) {
                    mLoadingDialog.dismiss();
                }
                if (mLoadingDialog != null && userInfoEntity != null && userInfoEntity.getData() != null) {
                    getActivity().finish();
                }
            }
        });
    }

    private void changeFragment(String provinceName, List<String> provinceList) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        hideFragments(transaction);
        if (provinceList == null) {
            alterFragment(transaction, RegionCountFragment.class);
            setNavigationTitle(getString(R.string.title_select_region));
        } else {
            mProvFragment = alterFragment(transaction, RegionProvFragment.class);
            Bundle args = new Bundle();
            args.putStringArrayList(EntranceConsts.KEY_PROLIST, new ArrayList<>(provinceList));
            if (mProvFragment != null) {
                mProvFragment.setArguments(args);
            }
            setNavigationTitle(provinceName);
        }
        transaction.commitAllowingStateLoss();
    }

    private <T extends Fragment> T alterFragment(FragmentTransaction transaction, Class<T> cls) {
        T fragmentByTag = (T) getChildFragmentManager().findFragmentByTag(cls.getSimpleName());
        try {
            if (fragmentByTag != null) {
                transaction.show(fragmentByTag);
            } else {
                fragmentByTag = cls.newInstance();
                transaction.add(R.id.layout_fragment_content, fragmentByTag, cls.getSimpleName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fragmentByTag;
    }

    @Override
    public boolean onHandleBackPressed() {

        if (!TextUtils.isEmpty(mProvinceName)) {
            mProvinceName = null;
            getChildFragmentManager().beginTransaction().remove(mProvFragment).commitAllowingStateLoss();
            changeFragment(null, null);
            return true;
        }

        return false;
    }

    @Override
    public void onRegionSelect(String content, List<String> provinceList) {
        if (provinceList == null) {
            String data;
            if (mProvinceName != null) {
                data = mProvinceName + " " + content;
            } else {
                data = content;
            }

            mLoadingDialog = DialogUtils.showWaitDialog(getActivity(), "正在修改信息...");
            mUserViewModel.changeUserInfo(data, UserViewModel.TYPE_REGION);
        } else {
            mProvinceName = content;
            changeFragment(content, provinceList);
        }
    }

}
