package ch.pizzalemon.android.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.api.ApiConstants;
import ch.pizzalemon.android.api.ApiErrorUtils;
import ch.pizzalemon.android.api.ApiService;
import ch.pizzalemon.android.api.ResponseObject;
import ch.pizzalemon.android.helper.DialogHelper;
import ch.pizzalemon.android.helper.SessionHelper;
import ch.pizzalemon.android.model.StoreInfoModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by cimenmus on 18/09/2017.
 */

public class StoreInfoFragment extends BaseFragment {

    @BindView(R.id.storeInfoWebView) WebView storeInfoWebView;

    @BindString(R.string.bottom_nav_menu_item_info) String fragmentTitle;

    public static final java.lang.String FRAGMENT_NAME = "infoFragment";

    public StoreInfoFragment() {}

    public static StoreInfoFragment newInstance() {
        return new StoreInfoFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_store_info, container, false);
        ButterKnife.bind(this, view);
        initViews();
        //getStoreInfo();
        initWebView();
        return view;
    }

    private void initViews(){
        setPizzalinkToolbarFields(false, getString(R.string.store_info_title));
    }

    /*
    private void getStoreInfo(){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject<StoreInfoModel>> call = ApiService.apiInterface.getStoreInfo(SessionHelper.getSelectedStore().getStoreName());
        call.enqueue(new Callback<ResponseObject<StoreInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseObject<StoreInfoModel>> call, Response<ResponseObject<StoreInfoModel>> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() &&
                        response.body().getData() != null &&
                        response.isSuccessful()){
                    initWebView(response.body().getData());
                }
                else {
                    ApiErrorUtils.parseError(response);
                }
            }

            @Override
            public void onFailure(Call<ResponseObject<StoreInfoModel>> call, Throwable t) {
                DialogHelper.hideLoadingDialog();
                DialogHelper.showFailedDialog();
            }
        });
    }
    */

    private void initWebView(StoreInfoModel storeInfoModel) {
        storeInfoModel.checkNull();
        storeInfoWebView.setVerticalScrollBarEnabled(false);
        storeInfoWebView.getSettings().setJavaScriptEnabled(true);
        storeInfoWebView.loadDataWithBaseURL("", createFixedHTML(storeInfoModel.getContent()), "text/html", "UTF-8", "");
    }

    private void initWebView(){
        storeInfoWebView.getSettings().setJavaScriptEnabled(true);
        storeInfoWebView.loadUrl(ApiConstants.STORE_INFO_WEB_PAGE_URL);
        storeInfoWebView.setVerticalScrollBarEnabled(false);
    }

    private String createFixedHTML(String html){


        // bunu kullanırsan, layout dosyasındaki yorum satırını aktif yap
        String header = "<head><style>img{display: inline; height: auto; max-width: 100%;} html, body { width:100%; height: 100%; margin: 0px; padding: 0px;}</style></head>";

        //String header = "<head><style>img{display: inline; height: auto; max-width: 100%;} html, body { width:98%; height: 100%; margin-left: 4px; padding: 0px;}</style></head>";

        //html = removeHs(html);

        /*
        String header = "<head>" +
                "        <meta http-equiv='content-type' content='text/html;'" +
                "        charset='UTF-8'>" +
                "            <style type='text/css'>" +
                "                " +
                "                @font-face {" +
                "                    font-family: 'myfont';" +
                "                    src: url('file:///android_asset/Resources/fonts/dosismedium.ttf');format('truetype'); " +
                "                }" +
                "                body { font-family:'myfont'; font-size: medium;}" +
                "                " +
                "               img{display: inline; height: auto; max-width: 100%;}" +
                "            </style>" +
                "    </head>";
                */



        return "<html>" + header + "<body>" + html + "</body></html>";
    }

}
