package com.kfzs.cfyl.media.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.CheckedTextView;

import com.kfzs.cfyl.media.BaseFragment;
import com.kfzs.cfyl.media.R;
import com.kfzs.cfyl.media.adapter.ViewPagerFragmentAdapter;
import com.kfzs.cfyl.media.util.G;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by realicing on 2018/12/3.
 * realicing@sina.com
 * 可在该界面选择滤镜、磨皮、大眼瘦脸的效果
 */
public class FgtChooseEditList extends BaseFragment {
    View fgt_choose_edit_list_ll;
    View fgt_choose_edit_line_v;
    View fgt_choose_edit_filter_tv;
    View fgt_choose_edit_buffing_tv;
    View fgt_choose_edit_warping_tv;
    ViewPager fgt_choose_edit_view_pager;

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

    @Override
    public void onViewCreated() {
        initView();
        initListener();
        initData();
    }

    private void initView() {
        fgt_choose_edit_list_ll = findViewById(R.id.media_fgt_choose_edit_list_ll);
        fgt_choose_edit_line_v = findViewById(R.id.media_fgt_choose_edit_line_v);
        fgt_choose_edit_view_pager = findViewById(R.id.media_fgt_choose_edit_view_pager);
        fgt_choose_edit_filter_tv = findViewById(R.id.media_fgt_choose_edit_filter_tv);
        fgt_choose_edit_buffing_tv = findViewById(R.id.media_fgt_choose_edit_buffing_tv);
        fgt_choose_edit_warping_tv = findViewById(R.id.media_fgt_choose_edit_warping_tv);
        lastView = fgt_choose_edit_filter_tv;
    }
    private void initListener() {
        fgt_choose_edit_filter_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSelectFilter(view);
            }
        });
        fgt_choose_edit_buffing_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSelectBuffing(view);
            }
        });
        fgt_choose_edit_warping_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSelectWarping(view);
            }
        });

    }
    private void initData() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new FgtChooseFilterList());
        fragmentList.add(new FgtChooseLevelList());
        ViewPagerFragmentAdapter mViewPagerFragmentAdapter = new ViewPagerFragmentAdapter(getChildFragmentManager(), fragmentList);
        try {
            fgt_choose_edit_view_pager.setAdapter(mViewPagerFragmentAdapter);
            fgt_choose_edit_view_pager.setOffscreenPageLimit(mViewPagerFragmentAdapter.getCount());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View lastView;
    //选择滤镜
    public void onSelectFilter(View view) {
        fgt_choose_edit_view_pager.setCurrentItem(0);
        changeCheckedState(view);
    }

    //选择磨皮
    public void onSelectBuffing(View view) {
        fgt_choose_edit_view_pager.setCurrentItem(1);
        changeCheckedState(view);
    }

    //选择大眼瘦脸
    public void onSelectWarping(View view) {
        G.showToast(getContext(), R.string.media_coming_soon);
    }

    private void changeCheckedState(View view) {
        if(lastView != view) {
            if (lastView instanceof CheckedTextView) {
                ((CheckedTextView) lastView).setChecked(false);
            }
            if (view instanceof CheckedTextView)
                ((CheckedTextView) view).setChecked(true);
            lastView = view;
        }
    }
}
