package com.itant.shiwushu.ui.menu.found;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.itant.shiwushu.MainActivity;
import com.itant.shiwushu.R;
import com.itant.shiwushu.base.BaseSkinFragment;
import com.itant.shiwushu.bean.City;
import com.itant.shiwushu.bean.LostFound;
import com.itant.shiwushu.bean.Province;
import com.itant.shiwushu.manager.CityManager;
import com.itant.shiwushu.manager.RealmManager;
import com.itant.shiwushu.manager.ToastManager;
import com.itant.shiwushu.tool.ActivityTool;
import com.itant.shiwushu.ui.city.ProvinceActivity;
import com.itant.shiwushu.ui.list.PostListActivity;
import com.itant.shiwushu.ui.menu.post.publish.PublishPostActivity;
import com.itant.shiwushu.ui.menu.sub.PostFragment;

import java.util.List;

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

public class FoundFragment extends BaseSkinFragment implements View.OnClickListener {

    private PostFragment foundFragment;
    private TextView tv_city_found;
    private EditText et_search_found;

    private int cityId = City.CITY_ALL;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_found;
    }

    @Override
    public void initViews(View view) {
        tv_city_found = view.findViewById(R.id.tv_city_found);
        et_search_found = view.findViewById(R.id.et_search_found);
        view.findViewById(R.id.ll_city_found).setOnClickListener(this);
        view.findViewById(R.id.aciv_add).setOnClickListener(this);
        view.findViewById(R.id.iv_search_found).setOnClickListener(this);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //实例化要管理的fragment
        foundFragment = new PostFragment();
        Bundle foundBundle = new Bundle();
        foundBundle.putInt(LostFound.KEY_POST_TYPE, LostFound.POST_TYPE_FOUND);
        foundFragment.setArguments(foundBundle);

        //通过添加（事务处理的方式）将fragment加到对应的布局中
        transaction.add(R.id.fragment_found, foundFragment);
        //事务处理完需要提交
        transaction.commit();
    }

    public void onCityChoose() {
        cityId = CityManager.getInstance().getFoundCity().getCityId();
        String cityName = CityManager.getInstance().getFoundCity().getName();
        tv_city_found.setText(cityName);
        if (cityId == 0) {
            et_search_found.setHint("在全国范围搜索招领启事");
        } else {
            et_search_found.setHint("在" + cityName + "搜索招领启事");
        }
        ToastManager.getInstance().toastShort("下拉刷新即可获取" + cityName + "的最新启事");
        foundFragment.onCityIdChanged(cityId);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_city_found:
                // 选择城市
                List<Province> provinceList = RealmManager.getInstance().getProvinceList();
                if (provinceList == null) {
                    ToastManager.getInstance().toastShort("城市加载失败");
                }
                Intent intent = new Intent(activity, ProvinceActivity.class);
                intent.putExtra(MainActivity.KEY_CITY_TYPE, MainActivity.CODE_CITY_FOUND);
                ActivityTool.startActivityForResult(activity, intent, MainActivity.CODE_CITY_FOUND);
                break;

            case R.id.aciv_add:
                // 新增招领启事
                Intent addFoundIntent = new Intent(activity, PublishPostActivity.class);
                addFoundIntent.putExtra(PublishPostActivity.KEY_POST_TYPE, PublishPostActivity.POST_TYPE_FOUND);
                ActivityTool.startActivity(activity, addFoundIntent);
                break;

            case R.id.iv_search_found:
                // 搜索
                String keywords = et_search_found.getText().toString();
                if (TextUtils.isEmpty(keywords)) {
                    ToastManager.getInstance().toastShort("请输入关键字");
                } else {
                    Intent searchIntent = new Intent(activity, PostListActivity.class);
                    searchIntent.putExtra(City.KEY_CITY, cityId);
                    searchIntent.putExtra(LostFound.KEY_POST_TYPE, LostFound.POST_TYPE_FOUND);
                    searchIntent.putExtra(PostFragment.KEY_KEYWORDS, keywords);
                    ActivityTool.startActivity(activity, searchIntent);
                }
                break;
        }
    }
}
