package com.itant.shiwushu.ui.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.itant.shiwushu.R;
import com.itant.shiwushu.base.BaseSkinActivity;
import com.itant.shiwushu.bean.City;
import com.itant.shiwushu.bean.LostFound;
import com.itant.shiwushu.ui.menu.sub.PostFragment;

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

public class PostListActivity extends BaseSkinActivity implements View.OnClickListener {
    private int type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_list);

        TextView tv_title = findViewById(R.id.tv_title);
        type = getIntent().getIntExtra(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_FAVORITE);
        String keywords = getIntent().getStringExtra(PostFragment.KEY_KEYWORDS);

        findViewById(R.id.aciv_back).setOnClickListener(this);

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

        if (TextUtils.isEmpty(keywords)) {
            if (type == LostFound.LOST_TYPE_PUBLISH) {
                tv_title.setText("我的发布");
                foundBundle.putInt(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_PUBLISH);
            } else {
                tv_title.setText("我的收藏");
                foundBundle.putInt(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_FAVORITE);
            }
        } else {
            tv_title.setText("搜索结果");
            int cityId = getIntent().getIntExtra(City.KEY_CITY, City.CITY_ALL);
            int postType = getIntent().getIntExtra(LostFound.KEY_POST_TYPE, LostFound.POST_TYPE_LOST);
            int lostType = getIntent().getIntExtra(LostFound.KEY_LOST_TYPE, LostFound.LOST_TYPE_ALL);

            foundBundle.putInt(City.KEY_CITY, cityId);
            foundBundle.putInt(LostFound.KEY_POST_TYPE, postType);
            foundBundle.putInt(LostFound.KEY_LOST_TYPE, lostType);
            foundBundle.putString(PostFragment.KEY_KEYWORDS, keywords);
        }

        foundFragment.setArguments(foundBundle);

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

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