package com.itant.shiwushu.ui.city;

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 android.widget.TextView;

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.City;
import com.itant.shiwushu.manager.CityManager;

import java.util.List;

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

public class CityActivity extends BaseSkinActivity implements View.OnClickListener {
    private RecyclerView rv_city;
    private List<City> cityList;
    private CommonAdapter<City> 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);

        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText("选择城市");

        rv_city = findViewById(R.id.rv_city);
        // 必须要设置LayoutManager，否则RecyclerView不知道要使用什么布局，从而在界面上不显示
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rv_city.setLayoutManager(layoutManager);
        cityList = CityManager.getInstance().getCityList();
        mAdapter = new CommonAdapter<City>(this, R.layout.item_city, cityList) {
            @Override
            protected void convert(final ViewHolder viewHolder, final City item, int position) {
                // 内容和时间
                viewHolder.setText(R.id.tv_name, item.getName());
                viewHolder.setOnClickListener(R.id.ll_post, new OnProvinceClickListener(item));
            }
        };
        rv_city.setAdapter(mAdapter);

        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 City city;
        public OnProvinceClickListener(City city) {
            this.city = city;
        }

        @Override
        public void onClick(View v) {
            switch (cityType) {
                case MainActivity.CODE_CITY_LOST:
                    CityManager.getInstance().setLostCity(city);
                    CityManager.getInstance().setCityList(null);
                    setResult(RESULT_OK);
                    finish();
                    break;
                case MainActivity.CODE_CITY_FOUND:
                    CityManager.getInstance().setFoundCity(city);
                    CityManager.getInstance().setCityList(null);
                    setResult(RESULT_OK);
                    finish();
                    break;

                case MainActivity.CODE_CITY_PUBLISH:
                    CityManager.getInstance().setPublishCity(city);
                    CityManager.getInstance().setCityList(null);
                    setResult(RESULT_OK);
                    finish();
                    break;
            }

        }
    }
}
