/*
Copyright 2015 shizhefei（LuckyJayce）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package com.zhaohaoting.framework.view;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhaohaoting.framework.mvchelper.mvc.BaseILoadView;
import com.zhaohaoting.framework.mvchelper.mvc.ILoadViewFactory;
import com.zhaohaoting.framework.mvchelper.view.vary.VaryViewHelperXV;
import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.utils.ToastUtils;


public class MyLoadViewFactory implements ILoadViewFactory {

    @Override
    public ILoadMoreView madeLoadMoreView() {
        return new LoadMoreHelper();
    }

    @Override
    public ILoadView madeLoadView() {
        return new LoadViewHelper();
    }

    private static class LoadMoreHelper implements ILoadMoreView {

        protected TextView footView;

        protected OnClickListener onClickRefreshListener;

        @Override
        public void init(FootViewAdder footViewHolder, OnClickListener onClickRefreshListener) {
            LinearLayout view = (LinearLayout) footViewHolder.addFootView(R.layout.layout_listview_foot);
            this.footView = view.findViewById(R.id.tv_footer);
            this.onClickRefreshListener = onClickRefreshListener;
            showLoading();
        }

        @Override
        public void showNormal() {
            footView.setText("点击加载更多");
            footView.setOnClickListener(onClickRefreshListener);
        }

        @Override
        public void showLoading() {
            footView.setText("正在加载中..");
            footView.setOnClickListener(null);
        }

        @Override
        public void showFail(Exception exception) {
            footView.setText("加载失败，点击重新加载");
            footView.setOnClickListener(onClickRefreshListener);
        }

        @Override
        public void showNomore() {
            footView.setText("已无更多数据");
            footView.setOnClickListener(null);
        }

    }

    private static class LoadViewHelper extends BaseILoadView {
        private VaryViewHelperXV helper;
        private OnClickListener onClickRefreshListener;
        private Context context;
        private String emptyTips;

        @Override
        public void init(View switchView, OnClickListener onClickRefreshListener) {
            this.context = switchView.getContext();
            this.onClickRefreshListener = onClickRefreshListener;
            helper = new VaryViewHelperXV(switchView);
            // TODO: 2017/12/28 默认显示 加载中
            showLoading();
        }

        @Override
        public void restore() {
            super.restore();
            helper.restoreView();
        }

        @Override
        public void setEmptyTips(String emptyTips) {
            super.setEmptyTips(emptyTips);
            this.emptyTips = emptyTips;
        }

        @Override
        public void showLoading() {
            if (currentShowStatus == SHOW_LOADING) return;
            super.showLoading();
            View layout = helper.inflate(R.layout.load_ing);
            TextView textView = (TextView) layout.findViewById(R.id.textView1);
            textView.setText("加载中...");
            helper.showLayout(layout);
            setCurrentShowStatus(SHOW_LOADING);
        }

        @Override
        public void tipFail(Exception exception) {
            super.tipFail(exception);
            ToastUtils.show(exception.getMessage());
        }

        @Override
        public void showFail(Exception exception) {
            super.showFail(exception);
            View layout = helper.inflate(R.layout.load_error);
            TextView textView = (TextView) layout.findViewById(R.id.error_tips);
            textView.setText(exception.getMessage());
            layout.setOnClickListener(onClickRefreshListener);
            helper.showLayout(layout);
        }

        @Override
        public void showEmpty() {
            super.showEmpty();
            View layout = helper.inflate(R.layout.load_empty);
            layout.setOnClickListener(onClickRefreshListener);
            helper.showLayout(layout);
        }

    }
}
