package com.itant.shiwushu.ui.city;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.itant.library.recyclerview.CommonAdapter;
import com.itant.library.recyclerview.base.ViewHolder;
import com.itant.shiwushu.MainActivity;
import com.itant.shiwushu.R;
import com.itant.shiwushu.base.BaseSkinActivity;
import com.itant.shiwushu.bean.Province;
import com.itant.shiwushu.manager.CityManager;
import com.itant.shiwushu.manager.RealmManager;
import com.itant.shiwushu.tool.ActivityTool;

import java.util.List;

/**
 * Created by Jason on 2018/10/7.
 */

public class ProvinceActivity extends BaseSkinActivity implements View.OnClickListener {
    private static final int CODE_SELECT_CITY = 1;
    private RecyclerView rv_province;
    private List<Province> provinceList;
    private CommonAdapter<Province> mAdapter;
    private int cityType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        findViewById(R.id.aciv_back).setOnClickListener(this);

        rv_province = findViewById(R.id.rv_city);
        // 必须要设置LayoutManager，否则RecyclerView不知道要使用什么布局，从而在界面上不显示
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rv_province.setLayoutManager(layoutManager);

        provinceList = RealmManager.getInstance().getProvinceList();
        mAdapter = new CommonAdapter<Province>(this, R.layout.item_city, provinceList) {
            @Override
            protected void convert(final ViewHolder viewHolder, final Province item, int position) {
                // 内容和时间
                viewHolder.setText(R.id.tv_name, item.getName());
                viewHolder.setOnClickListener(R.id.ll_post, new OnProvinceClickListener(item));
            }
        };
        rv_province.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

        cityType = getIntent().getIntExtra(MainActivity.KEY_CITY_TYPE, MainActivity.CODE_CITY_LOST);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.aciv_back:
                finish();
                break;
        }
    }

    private class OnProvinceClickListener implements View.OnClickListener {
        private Province province;
        public OnProvinceClickListener(Province province) {
            this.province = province;
        }

        @Override
        public void onClick(View v) {
            CityManager.getInstance().setCityList(province.getCity());
            Intent intent = new Intent(ProvinceActivity.this, CityActivity.class);
            intent.putExtra(MainActivity.KEY_CITY_TYPE, cityType);
            ActivityTool.startActivityForResult(ProvinceActivity.this, intent, CODE_SELECT_CITY);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }
}
