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

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

import com.flyco.tablayout.SlidingTabLayout;
import com.itant.shiwushu.MainActivity;
import com.itant.shiwushu.R;
import com.itant.shiwushu.adapter.TabPageAdapter;
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.ArrayList;
import java.util.List;

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

public class LostFragment extends BaseSkinFragment implements View.OnClickListener {
    private List<PostFragment> fragments;
    private ViewPager vp_main;
    private SlidingTabLayout tl_1;
    private TextView tv_city_lost;
    private EditText et_search_lost;
    private int cityId = City.CITY_ALL;
    private int lostType = LostFound.LOST_TYPE_ALL;

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

    @Override
    public void initViews(View view) {
        view.findViewById(R.id.ll_city_lost).setOnClickListener(this);
        view.findViewById(R.id.aciv_add).setOnClickListener(this);
        view.findViewById(R.id.iv_search_lost).setOnClickListener(this);

        tv_city_lost = view.findViewById(R.id.tv_city_lost);
        et_search_lost = view.findViewById(R.id.et_search_lost);

        String[] titles = {"最新", "悬赏", "寻人", "找物", "爱宠"};
        vp_main = view.findViewById(R.id.vp_main);

        fragments = new ArrayList<>();
        // 全部
        PostFragment allLostFragment = new PostFragment();
        Bundle allLostBundle = new Bundle();
        allLostBundle.putInt(LostFound.KEY_POST_TYPE, LostFound.POST_TYPE_LOST);
        allLostBundle.putInt(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_ALL);
        allLostFragment.setArguments(allLostBundle);
        fragments.add(allLostFragment);

        // 悬赏
        PostFragment moneyLostFragment = new PostFragment();
        Bundle moneyLostBundle = new Bundle();
        moneyLostBundle.putInt(LostFound.KEY_POST_TYPE, LostFound.POST_TYPE_LOST);
        moneyLostBundle.putInt(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_MONEY);
        moneyLostFragment.setArguments(moneyLostBundle);
        fragments.add(moneyLostFragment);

        // 寻人
        PostFragment manLostFragment = new PostFragment();
        Bundle manLostBundle = new Bundle();
        manLostBundle.putInt(LostFound.KEY_POST_TYPE, LostFound.POST_TYPE_LOST);
        manLostBundle.putInt(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_MAN);
        manLostFragment.setArguments(manLostBundle);
        fragments.add(manLostFragment);

        // 寻物
        PostFragment goodsLostFragment = new PostFragment();
        Bundle goodsLostBundle = new Bundle();
        goodsLostBundle.putInt(LostFound.KEY_POST_TYPE, LostFound.POST_TYPE_LOST);
        goodsLostBundle.putInt(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_GOODS);
        goodsLostFragment.setArguments(goodsLostBundle);
        fragments.add(goodsLostFragment);

        // 寻物
        PostFragment petsLostFragment = new PostFragment();
        Bundle petsLostBundle = new Bundle();
        petsLostBundle.putInt(LostFound.KEY_POST_TYPE, LostFound.POST_TYPE_LOST);
        petsLostBundle.putInt(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_PETS);
        petsLostFragment.setArguments(petsLostBundle);
        fragments.add(petsLostFragment);

        TabPageAdapter adapter = new TabPageAdapter(getChildFragmentManager(), fragments);
        vp_main.setAdapter(adapter);
        vp_main.setOffscreenPageLimit(titles.length);
        vp_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 1:
                        lostType = LostFound.LOST_TYPE_MONEY;
                        break;
                    case 2:
                        lostType = LostFound.LOST_TYPE_MAN;
                        break;
                    case 3:
                        lostType = LostFound.LOST_TYPE_GOODS;
                        break;

                    case 4:
                        lostType = LostFound.LOST_TYPE_PETS;
                        break;

                    default:
                        lostType = LostFound.LOST_TYPE_ALL;
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tl_1 = view.findViewById(R.id.tl_1);
        tl_1.setViewPager(vp_main, titles);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_city_lost:
                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_LOST);
                ActivityTool.startActivityForResult(activity, intent, MainActivity.CODE_CITY_LOST);
                break;

            case R.id.aciv_add:
                Intent addLostIntent = new Intent(activity, PublishPostActivity.class);
                addLostIntent.putExtra(PublishPostActivity.KEY_POST_TYPE, PublishPostActivity.POST_TYPE_LOST);
                ActivityTool.startActivity(activity, addLostIntent);
                break;

            case R.id.iv_search_lost:
                // 搜索
                String keywords = et_search_lost.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_LOST);
                    searchIntent.putExtra(LostFound.KEY_LOST_TYPE, lostType);
                    searchIntent.putExtra(PostFragment.KEY_KEYWORDS, keywords);
                    ActivityTool.startActivity(activity, searchIntent);
                }
                break;
        }
    }

    public void onCityChoose() {
        // 重新选择了城市
        String cityName = CityManager.getInstance().getLostCity().getName();
        tv_city_lost.setText(cityName);
        cityId = CityManager.getInstance().getLostCity().getCityId();
        if (cityId == 0) {
            et_search_lost.setHint("在全国范围搜索失物启事");
        } else {
            et_search_lost.setHint("在" + cityName + "搜索失物启事");
        }

        ToastManager.getInstance().toastShort("下拉刷新即可获取" + cityName + "的最新启事");
        for (PostFragment fragment : fragments) {
            fragment.onCityIdChanged(cityId);
        }
    }
}
