package com.halo.assistant.fragment.user.region;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gh.gamecenter.core.utils.GsonUtils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.AreaViewHolder;
import com.gh.gamecenter.databinding.AreaItemBinding;
import com.gh.gamecenter.entity.LocalEntity;
import com.gh.gamecenter.login.user.UserManager;
import com.google.gson.reflect.TypeToken;
import com.lightgame.adapter.BaseRecyclerAdapter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by khy on 25/09/17.
 */

public class RegionCounAdapter extends BaseRecyclerAdapter {

    private OnRegionSelectListener mListener;

    private List<LocalEntity> mLocalList;

    private String mCurRegin;

    public RegionCounAdapter(Context context, OnRegionSelectListener listener) {
        super(context);
        mListener = listener;
        mLocalList = new ArrayList<>();
        mCurRegin = UserManager.getInstance().getUserInfoEntity().getRegion();
        initData();
    }

    private void initData() {
        String locList = "";
        try (InputStreamReader inputReader = new InputStreamReader(mContext.getAssets().open("LocList.txt"));
             BufferedReader bufReader = new BufferedReader(inputReader)) {
            String line;
            while ((line = bufReader.readLine()) != null) {
                locList += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (!TextUtils.isEmpty(locList)) {
            Type listType = new TypeToken<List<LocalEntity>>() {
            }.getType();
            List<LocalEntity> localList = GsonUtils.getGson().fromJson(locList, listType);
            mLocalList.addAll(localList);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemViewType(int position) {
        String name = mLocalList.get(position).getName();
        if (("国内".equals(name) || "国外".equals(name))) {
            return 1;
        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if (viewType == 0) {
            return new AreaViewHolder(AreaItemBinding.inflate(mLayoutInflater, viewGroup, false));
        } else {
            return new AreaTitleViewHolder(mLayoutInflater.inflate(R.layout.area_title_item, viewGroup, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        LocalEntity localEntity = mLocalList.get(position);
        final String name = localEntity.getName();
        if (holder instanceof AreaViewHolder) {
            final List<String> city = localEntity.getCity();
            AreaViewHolder viewHolder = (AreaViewHolder) holder;

            if (city != null && city.size() > 0) {
                viewHolder.binding.areaItemMore.setVisibility(View.VISIBLE);
                viewHolder.binding.areaItemSelect.setVisibility(View.GONE);
            } else {
                viewHolder.binding.areaItemMore.setVisibility(View.GONE);

                if (!TextUtils.isEmpty(mCurRegin) && mCurRegin.contains(name)) {
                    viewHolder.binding.areaItemSelect.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.binding.areaItemSelect.setVisibility(View.GONE);
                }
            }
            viewHolder.binding.areaItemName.setText(name);
            viewHolder.binding.getRoot().setOnClickListener(view -> mListener.onRegionSelect(name, city));
        } else if (holder instanceof AreaTitleViewHolder) {
            ((AreaTitleViewHolder) holder).name.setText(name);
        }
    }

    @Override
    public int getItemCount() {
        return mLocalList.size();
    }

    private class AreaTitleViewHolder extends RecyclerView.ViewHolder {
        TextView name;

        private AreaTitleViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView;
        }
    }
}
